H
hn.ft.pris
Hi:
I'm a beginer of STL, and I'm wondering why none of below works:
########################################################################
..........
string str("string");
if ( str[0] == "s" ) cout << "First character is s" << endl;
OR:
string str("string");
string::iterator it = str.begin();
if ((*it) == "s") cout << "First character is s" << endl;
OR:
string str("string");
if ( str[0] == (const char*)"s" ) cout << "First character is s" <<
endl;
#######################################################################
So, what should I do if I want to use operator== or operator[] to judge
whether one of the character in a string equals an special character? I
know c_str() may work, but why couldn't my method fails compilation?
Thanks!
I'm a beginer of STL, and I'm wondering why none of below works:
########################################################################
..........
string str("string");
if ( str[0] == "s" ) cout << "First character is s" << endl;
OR:
string str("string");
string::iterator it = str.begin();
if ((*it) == "s") cout << "First character is s" << endl;
OR:
string str("string");
if ( str[0] == (const char*)"s" ) cout << "First character is s" <<
endl;
#######################################################################
So, what should I do if I want to use operator== or operator[] to judge
whether one of the character in a string equals an special character? I
know c_str() may work, but why couldn't my method fails compilation?
Thanks!