473,796 Members | 2,495 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 3232
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
5855
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
2253
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
1632
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
1342
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
17132
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
1832
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
9683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9529
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10231
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...
0
10013
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
9054
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7550
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5443
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
5576
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2927
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.