Beginer's question about string::operator==

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!
 
R

Rolf Magnus

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;

str[0] is of type char. "s" is of type pointer to const char (or rather
decays into one).
OR:
string str("string");
string::iterator it = str.begin();
if ((*it) == "s") cout << "First character is s" << endl;

Same as above.
OR:
string str("string");
if ( str[0] == (const char*)"s" ) cout << "First character is s" <<
endl;

And again.
#######################################################################
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?

Try:

if ( str[0] == 's' ) cout << "First character is s" << endl;
 
R

[rob desbois]

string::eek:perator[] returns a char not a string object - your code is
effectively trying to perform:
if ('s' == "s")
hence you will get an error about trying to compare a pointer to a
string/object.

You need to do:
if ( str[0] == 's' ) cout << "First character is s" << endl;

Notice that I have changed "s" to 's'.
As an aside, if you try str[n] where n is a non-existant index in str
you will get no error, I'd suggest using str.at(n) unless you are
certain that you will be accessing an existant index.

--Rob
 
R

ruka

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!
 
D

Daniel T.

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?

str[0] is a 'char', not a 'const char*'. A character literal is denoted
by single quotes, not double quotes. In every case above, if you replace
"s" with 's', (and remove the cast in the last case) it should work.
 
H

Harry

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!

In all the above statements U are trying to compare a "char" with
"const char *".
All the above statements will give the following error:
"ComeauTest.c", line 6: error: operand types are incompatible ("char"
and
"const char *")
if ( str[0] == "s" ) cout << "First character is s" << endl;
^

"ComeauTest.c", line 8: error: operand types are incompatible ("char"
and
"const char *")
if ((*it) == "s") cout << "First character is s" << endl;
^

"ComeauTest.c", line 9: error: operand types are incompatible ("char"
and
"const char *")
if ( str[0] == (const char*)"s" ) cout << "First character is s"
<<endl;
^

3 errors detected in the compilation of "ComeauTest.c".

Try this:

if ( str[0] == 's' ) cout << "First character is s" << endl;
string::iterator it = str.begin();

if ((*it) == 's') cout << "First character is s" << endl;

Thanks.
HP~You are the creator of your own destiny~
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top