473,804 Members | 2,191 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Beginer's question about string::operato r==

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::iterato r 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!

Dec 5 '06 #1
5 3235
hn********@gmai l.com wrote:
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::iterato r 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;

Dec 5 '06 #2
string::operato r[] 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
hn********@gmai l.com wrote:
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::iterato r 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!
Dec 5 '06 #3

hn********@gmai l.com schrieb:
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::iterato r 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!
Dec 5 '06 #4
In article <11************ **********@80g2 000cwy.googlegr oups.com>,
hn********@gmai l.com wrote:
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::iterato r 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.

--
To send me email, put "sheltie" in the subject.
Dec 5 '06 #5

hn********@gmai l.com wrote:
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::iterato r 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::iterato r it = str.begin();

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

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

Dec 6 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5858
by: Matthias Käppler | last post by:
Hi, I just browsed libstdc++6-doc and stumbled over a string operation I haven't noticed before: string::push_back. If for example I want to append single characters to an std::string, which one would be better, or is there any difference at all: mystr.push_back( c ); or
4
2254
by: Matthias | last post by:
Now I am confused. I am doing a "less" comparison in my program between strings, to see which strings in some range precede others. This is my approach: class SortedByName { public: bool operator() (const boost::filesystem::path& lhs, const boost::filesystem::path& rhs) {
3
1634
by: belief | last post by:
I write a non-template simple string with the technique that giving the same string a same storage with a count. so i must give the necessary copy operation when the string will be modified, I differ the two kinds of operator s. // const object to use as const const char& operator const { //directly get the ref
1
1343
by: Tony Johansson | last post by:
Hello! I have this wrapper class Integer below that I use when testing operator overloading. A book that I read say that the expression Integer i; i+5 is translated to operator+(i,5) using the stand-alone helper function. If Integer's constructor were not declared as explicit, the previous would have one more possible translation which is operator+(i, Integer(5))
3
17134
by: jl_post | last post by:
Hi, I recently wrote two benchmark programs that compared if two strings were equal: one was a C program that used C char arrays with strcmp(), and the other was a C++ program that used std::strings with operator==(). In both programs, the first string consisted of one million characters (all the letter 'a'). The second string was always one character longer than the first string (with the letter 'a' for all the
7
1833
by: Eckhard Lehmann | last post by:
Hi, I try to recall some C++ currently. Therefore I read the "Standard C++ Bible" by C. Walnum, A. Stevens and - of course there are chapters about operator overloading. Now I have a class xydata with operator- overloaded: class xydata { public:
4
1309
by: Harlan Messinger | last post by:
Since operator overloads into static functions in C#, there really doesn't need to be a connection between the types to which the operator is being applied and the type in which the overload is defined, does there? I mean, there's nothing to prevent the following, right? public class A { public static B operator + (C c, D d) { ... } ... }
2
3090
by: www.brook | last post by:
I want to enfore the implementation of the operator overloading eg class Interface_ { public: virtual void operator = (const Interface &other)=0; virtual void operator +=(const Interface &other)=0; } but in the sub class
6
1232
by: Rafael Anschau | last post by:
In this case, how does the compiler knows which to run, given that both operators receive the same parameter ? class String { // overloaded operators char & operator(int offset); char operator(int offset) const;
0
10603
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10353
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10356
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10099
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3836
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3003
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.