Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › What does this line of C++ code mean?
New Posts  All Forums:Forum Nav:

What does this line of C++ code mean?

post #1 of 9
Thread Starter 
I was reading one of my C++ books to refresh on coding, and I came across this line

Code:
if (THE_WORD.find(guess) != string::npos)
Now I know it is just a simple if statement, but the test expression I don't understand, and the book gives a very bad explanation (to the point where I don't get it )

So can someone explain to me this test expression? Thanks.
Dolans Mate
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 760 Gigabyte P55A-UD3 Gainward GTX 560Ti Mushkin Radioactive (4GB) 
Hard DriveCoolingOSMonitor
160GB Seagate x 2 Cooler Master Hyper 212 EVO Windows 7 Ultimate 64-bit LG LED Flatron E2250V x 2 
KeyboardPowerCaseMouse Pad
Das Keyboard Utimate S  Silverstone Strider 500w NZXT Guardian 921 Razer Goliathus Fragged Alpha 
Audio
Logitech X-540s 
  hide details  
Reply
Dolans Mate
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 760 Gigabyte P55A-UD3 Gainward GTX 560Ti Mushkin Radioactive (4GB) 
Hard DriveCoolingOSMonitor
160GB Seagate x 2 Cooler Master Hyper 212 EVO Windows 7 Ultimate 64-bit LG LED Flatron E2250V x 2 
KeyboardPowerCaseMouse Pad
Das Keyboard Utimate S  Silverstone Strider 500w NZXT Guardian 921 Razer Goliathus Fragged Alpha 
Audio
Logitech X-540s 
  hide details  
Reply
post #2 of 9
Any further details? Like for example what type of variables these are?

EDIT: on a side note... Dragon Ball Z?
Edited by steelbom - 8/21/11 at 5:10am
Kasuf
(9 items)
 
  
CPUMotherboardGraphicsRAM
Core i5 6600K ASRock Z170 Pro4 ASUS Radeon RX 480 ROG Strix Corsair Vengeance LPX 32GB 3000MHz 
Hard DriveCoolingMonitorPower
Samsung 850 EVO Noctua NH-D15 LG 34" Ultrawide (LG34UC98) Corsair HX750i 
Case
Silverstone FT05B-W 
  hide details  
Reply
Kasuf
(9 items)
 
  
CPUMotherboardGraphicsRAM
Core i5 6600K ASRock Z170 Pro4 ASUS Radeon RX 480 ROG Strix Corsair Vengeance LPX 32GB 3000MHz 
Hard DriveCoolingMonitorPower
Samsung 850 EVO Noctua NH-D15 LG 34" Ultrawide (LG34UC98) Corsair HX750i 
Case
Silverstone FT05B-W 
  hide details  
Reply
post #3 of 9
Quote:
Originally Posted by Uncle Dolans View Post
I was reading one of my C++ books to refresh on coding, and I came across this line

Code:
if (THE_WORD.find(guess) != string::npos)
Now I know it is just a simple if statement, but the test expression I don't understand, and the book gives a very bad explanation (to the point where I don't get it )

So can someone explain to me this test expression? Thanks.
npos is a constant of -1, meaning false. So your expression means that if "guess" is not found in THE_WORD, then do whatever's next.
Logic
(14 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K ASRock P67 Extreme4 Gen3 ASUS HD 7950 TWIN FROZR 3GB Corsair Vengeance 16GB DDR3-1600 
Hard DriveCoolingOSMonitor
Intel 330 120GB SSD + Spinpoint F3 1TB Corsair H60 Windows 7 Ultimate x64 Samsung 24.6" P2570HD 
KeyboardPowerCaseMouse
Filco Ninja Majestouch-2 Tenkeyless Antec EarthWatts 650W Corsair 600T Silver Logitech G500 
  hide details  
Reply
Logic
(14 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K ASRock P67 Extreme4 Gen3 ASUS HD 7950 TWIN FROZR 3GB Corsair Vengeance 16GB DDR3-1600 
Hard DriveCoolingOSMonitor
Intel 330 120GB SSD + Spinpoint F3 1TB Corsair H60 Windows 7 Ultimate x64 Samsung 24.6" P2570HD 
KeyboardPowerCaseMouse
Filco Ninja Majestouch-2 Tenkeyless Antec EarthWatts 650W Corsair 600T Silver Logitech G500 
  hide details  
Reply
post #4 of 9
Thread Starter 
Quote:
Originally Posted by wuddersup View Post
npos is a constant of -1, meaning false. So your expression means that if "guess" is not found in THE_WORD, then do whatever's next.
Ah thanks. Basically i just wanted to know what 'npos' was doing.

Quote:
Originally Posted by steelbom View Post
Any further details? Like for example what type of variables these are?

EDIT: on a side note... Dragon Ball Z?
Well i would say what npos means but wudder answered for me.
Also, what about DBZ? My sig rig is named after the android if that is what you mean
Dolans Mate
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 760 Gigabyte P55A-UD3 Gainward GTX 560Ti Mushkin Radioactive (4GB) 
Hard DriveCoolingOSMonitor
160GB Seagate x 2 Cooler Master Hyper 212 EVO Windows 7 Ultimate 64-bit LG LED Flatron E2250V x 2 
KeyboardPowerCaseMouse Pad
Das Keyboard Utimate S  Silverstone Strider 500w NZXT Guardian 921 Razer Goliathus Fragged Alpha 
Audio
Logitech X-540s 
  hide details  
Reply
Dolans Mate
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 760 Gigabyte P55A-UD3 Gainward GTX 560Ti Mushkin Radioactive (4GB) 
Hard DriveCoolingOSMonitor
160GB Seagate x 2 Cooler Master Hyper 212 EVO Windows 7 Ultimate 64-bit LG LED Flatron E2250V x 2 
KeyboardPowerCaseMouse Pad
Das Keyboard Utimate S  Silverstone Strider 500w NZXT Guardian 921 Razer Goliathus Fragged Alpha 
Audio
Logitech X-540s 
  hide details  
Reply
post #5 of 9
Quote:
Originally Posted by Uncle Dolans View Post
Well i would say what npos means but wudder answered for me.
Also, what about DBZ? My sig rig is named after the android if that is what you mean
Aha I thought as much =)
Kasuf
(9 items)
 
  
CPUMotherboardGraphicsRAM
Core i5 6600K ASRock Z170 Pro4 ASUS Radeon RX 480 ROG Strix Corsair Vengeance LPX 32GB 3000MHz 
Hard DriveCoolingMonitorPower
Samsung 850 EVO Noctua NH-D15 LG 34" Ultrawide (LG34UC98) Corsair HX750i 
Case
Silverstone FT05B-W 
  hide details  
Reply
Kasuf
(9 items)
 
  
CPUMotherboardGraphicsRAM
Core i5 6600K ASRock Z170 Pro4 ASUS Radeon RX 480 ROG Strix Corsair Vengeance LPX 32GB 3000MHz 
Hard DriveCoolingMonitorPower
Samsung 850 EVO Noctua NH-D15 LG 34" Ultrawide (LG34UC98) Corsair HX750i 
Case
Silverstone FT05B-W 
  hide details  
Reply
post #6 of 9
Quote:
Originally Posted by wuddersup View Post
npos is a constant of -1, meaning false. So your expression means that if "guess" is not found in THE_WORD, then do whatever's next.
Actually his expression means if "guess" IS found in the word, as it's checking to see if the position returned by find() was not invalid.
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
post #7 of 9
Thread Starter 
Quote:
Originally Posted by lordikon View Post
Actually his expression means if "guess" IS found in the word, as it's checking to see if the position returned by find() was not invalid.
Well yeah. I can understand the rest of the expression, just was confused on what npos meant.
Dolans Mate
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 760 Gigabyte P55A-UD3 Gainward GTX 560Ti Mushkin Radioactive (4GB) 
Hard DriveCoolingOSMonitor
160GB Seagate x 2 Cooler Master Hyper 212 EVO Windows 7 Ultimate 64-bit LG LED Flatron E2250V x 2 
KeyboardPowerCaseMouse Pad
Das Keyboard Utimate S  Silverstone Strider 500w NZXT Guardian 921 Razer Goliathus Fragged Alpha 
Audio
Logitech X-540s 
  hide details  
Reply
Dolans Mate
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 760 Gigabyte P55A-UD3 Gainward GTX 560Ti Mushkin Radioactive (4GB) 
Hard DriveCoolingOSMonitor
160GB Seagate x 2 Cooler Master Hyper 212 EVO Windows 7 Ultimate 64-bit LG LED Flatron E2250V x 2 
KeyboardPowerCaseMouse Pad
Das Keyboard Utimate S  Silverstone Strider 500w NZXT Guardian 921 Razer Goliathus Fragged Alpha 
Audio
Logitech X-540s 
  hide details  
Reply
post #8 of 9
Quote:
Originally Posted by Uncle Dolans View Post
Well yeah. I can understand the rest of the expression, just was confused on what npos meant.
Ah ok. A great place to find details about methods like .find is www.cplusplus.com. For example, this is probably everything you needed to know about string to figure out the npos example you gave:

http://www.cplusplus.com/reference/string/string/find/
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
post #9 of 9
Thread Starter 
Quote:
Originally Posted by lordikon View Post
Ah ok. A great place to find details about methods like .find is www.cplusplus.com. For example, this is probably everything you needed to know about string to figure out the npos example you gave:

http://www.cplusplus.com/reference/string/string/find/
Hmm ok thanks.
Dolans Mate
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 760 Gigabyte P55A-UD3 Gainward GTX 560Ti Mushkin Radioactive (4GB) 
Hard DriveCoolingOSMonitor
160GB Seagate x 2 Cooler Master Hyper 212 EVO Windows 7 Ultimate 64-bit LG LED Flatron E2250V x 2 
KeyboardPowerCaseMouse Pad
Das Keyboard Utimate S  Silverstone Strider 500w NZXT Guardian 921 Razer Goliathus Fragged Alpha 
Audio
Logitech X-540s 
  hide details  
Reply
Dolans Mate
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 760 Gigabyte P55A-UD3 Gainward GTX 560Ti Mushkin Radioactive (4GB) 
Hard DriveCoolingOSMonitor
160GB Seagate x 2 Cooler Master Hyper 212 EVO Windows 7 Ultimate 64-bit LG LED Flatron E2250V x 2 
KeyboardPowerCaseMouse Pad
Das Keyboard Utimate S  Silverstone Strider 500w NZXT Guardian 921 Razer Goliathus Fragged Alpha 
Audio
Logitech X-540s 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › What does this line of C++ code mean?