在这里,我将演示如何从 std::string 中找到一个*问号('?'):请务必阅读评论!
int main( void )
{
// DECLARE LOCAL VARIABLES
// Declare strA as std::string
std::string strA = "";
// Declare nIndex as int. We set the value of it to -1 to let us know
// if there were any '?' within strA
int nIndex = -1;
// INITIALIZE
// Set the value of strA to the line that was inputted
// You might want to loop this until the length of strA
// is greater than 0
std::getline( std::cin, strA );
// SEARCH FOR THE FIRST '?'
// if std::string::find returns -1, then no '?' was found within strA
nIndex = strA.find( '?' );
// CHECKING AND PRINTING nIndex
// Remember why strA is -1?
if( nIndex == -1 )
std::cout << "The inputted string does not contain any '?'...\n";
else std::cout << "The first '?' is found at: " << nIndex;
// DONE
#ifdef DEBUG
std::cout << "\nDebugging > Paused! Enter any key to continue...\n";
::getchar( );
#endif // DEBUG
return( 0 );
};