473,386 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

compare string and "" string literal

Is it correct and safe to compare a string object with "", a pair of
quotation marks quoted empty string?If the string object: s = ""; does
s contain a single '\'? Is it better to use std::string::size or
std::string::empty to deal with this condition? Thank you.

string s = "";
if (s == "");
if (s.size == 0);
if (s.empty());

Jan 24 '07 #1
10 9017


On 1ÔÂ24ÈÕ, ÉÏÎç10ʱ41·Ö, "lovecreatesbea...@gmail.com"
<lovecreatesbea...@gmail.comwrote:
Is it correct and safe to compare a string object with "", a pair of
quotation marks quoted empty string?If the string object: s = ""; does
s contain a single '\'? Is it better to use std::string::size or
std::string::empty to deal with this condition? Thank you.

string s = "";
if (s == "");
if (s.size == 0);
if (s.empty());
(sorry for the mistakes in my previous post)

Is it correct and safe to compare a string object with "", a pair of
quotation marks quoted empty string? If the string object: s = ""; does
s contain a single '\0'? Is it better to use std::string::size or
std::string::empty to deal with this condition?

string s = "";
if (s == "");
if (s.size == 0);
if (s.empty());

Jan 24 '07 #2
lovecreatesbea...@gmail.com a ¨¦crit :
>

Is it correct and safe to compare a string object with "", a pair of
quotation marks quoted empty string?
Yes, the compiler creates a temporary string initialized with "" to
perform the comparison.
If the string object: s = ""; does
s contain a single '\0'?
This is implementation depedant, the only requirement is that the
c_str() method returns a POD char* which is 0 terminated.
Is it better to use std::string::size or
std::string::empty to deal with this condition?
Unless you have a specific reason to do otherwise, the
std::string::empty is the clearer.
>
string s = "";
if (s == "");
Costs a intermediary object and a call to a comparison function unless
there is some kind of optimisation (?).
if (s.size == 0);
It is to note that std::string::size complexity may be linear in the
container's size; but it should work fine with main string implementations.
if (s.empty());
It may be equivalent to a.size() == 0 but it is expected to run in
amortized constant time; it may be faster if that does count.
Michael
Jan 24 '07 #3
On Wed, 24 Jan 2007 08:53:19 +0100, Michael DOUBEZ wrote:
>lovecreatesbea...@gmail.com a ¨¦crit :
>Is it correct and safe to compare a string object with "", a pair of
quotation marks quoted empty string?

Yes, the compiler creates a temporary string initialized with "" to
perform the comparison.
The compiler does not create a temporary string. Some implementations
internally create a temporary string but that is a different question.

Best wishes,
Roland Pibinger
Jan 24 '07 #4
Roland Pibinger a écrit :
On Wed, 24 Jan 2007 08:53:19 +0100, Michael DOUBEZ wrote:
>lovecreatesbea...@gmail.com a ¨¦crit :
>>Is it correct and safe to compare a string object with "", a pair of
quotation marks quoted empty string?
Yes, the compiler creates a temporary string initialized with "" to
perform the comparison.

The compiler does not create a temporary string. Some implementations
internally create a temporary string but that is a different question.
Which implementation are you talking about, the compiler's or the
library's ?

The standard defines only:
bool std::string::operator==(const string& c1, const string& c2);

So encoutering:
s == "blah"

The compiler matched "blah" as a string through the relevant constructor:
std::string::string( const char* str );

And uses an intermediary object. Now, dependending on the implementation
of operator== and the constructor, the actual instanciation of a
std::string may be avoided by the compiler. Is it what you mean ?

Regards,
Michael



Jan 24 '07 #5
On Wed, 24 Jan 2007 10:29:00 +0100, Michael DOUBEZ wrote:
>Roland Pibinger a écrit :
>On Wed, 24 Jan 2007 08:53:19 +0100, Michael DOUBEZ wrote:
>>lovecreatesbea...@gmail.com a ¨¦crit :
Is it correct and safe to compare a string object with "", a pair of
quotation marks quoted empty string?
Yes, the compiler creates a temporary string initialized with "" to
perform the comparison.

The compiler does not create a temporary string. Some implementations
internally create a temporary string but that is a different question.

Which implementation are you talking about, the compiler's or the
library's ?
the library
>The standard defines only:
bool std::string::operator==(const string& c1, const string& c2);
In 21.2 the C++ Standard defines the following function templates:

template<class charT, class traits, class Allocator>
bool operator==(const basic_string<charT,traits,Allocator>& lhs,
const basic_string<charT,traits,Allocator>& rhs);

template<class charT, class traits, class Allocator>
bool operator==(const charT* lhs,
const basic_string<charT,traits,Allocator>& rhs);

template<class charT, class traits, class Allocator>
bool operator==(const basic_string<charT,traits,Allocator>& lhs,
const charT* rhs);

Implementations of the latter two templates need not create a
temporary string object.

Best regards,
Roland Pibinger
Jan 24 '07 #6
Roland Pibinger a écrit :
On Wed, 24 Jan 2007 10:29:00 +0100, Michael DOUBEZ wrote:
>Roland Pibinger a écrit :
>>On Wed, 24 Jan 2007 08:53:19 +0100, Michael DOUBEZ wrote:
lovecreatesbea...@gmail.com a ¨¦crit :
Is it correct and safe to compare a string object with "", a pair of
quotation marks quoted empty string?
Yes, the compiler creates a temporary string initialized with "" to
perform the comparison.
The compiler does not create a temporary string. Some implementations
internally create a temporary string but that is a different question.
Which implementation are you talking about, the compiler's or the
library's ?

the library
>The standard defines only:
bool std::string::operator==(const string& c1, const string& c2);

In 21.2 the C++ Standard defines the following function templates:

template<class charT, class traits, class Allocator>
bool operator==(const basic_string<charT,traits,Allocator>& lhs,
const basic_string<charT,traits,Allocator>& rhs);

template<class charT, class traits, class Allocator>
bool operator==(const charT* lhs,
const basic_string<charT,traits,Allocator>& rhs);

template<class charT, class traits, class Allocator>
bool operator==(const basic_string<charT,traits,Allocator>& lhs,
const charT* rhs);

Implementations of the latter two templates need not create a
temporary string object.
You are right, I had never seen it.
But then, what about section 21.3.8.2:

template<class charT, class traits, class Allocator>
bool operator==(const basic_string<charT,traits,Allocator>& lhs,
const basic_string<charT,traits,Allocator>& rhs);

1 Returns:lhs.compare(rhs) == 0.

template<class charT, class traits, class Allocator>
bool operator==(const charT* lhs,
const basic_string<charT,traits,Allocator>& rhs);

2 Returns:basic_string<charT,traits,Allocator>(lhs) == rhs.

template<class charT, class traits, class Allocator>
bool operator==(const basic_string<charT,traits,Allocator>& lhs,
const charT* rhs);

3 Returns:lhs == basic_string<charT,traits,Allocator>(rhs).
Michael

Jan 24 '07 #7
On Wed, 24 Jan 2007 11:14:46 +0100, Michael DOUBEZ wrote:
>But then, what about section 21.3.8.2:

template<class charT, class traits, class Allocator>
bool operator==(const basic_string<charT,traits,Allocator>& lhs,
const charT* rhs);

3 Returns:lhs == basic_string<charT,traits,Allocator>(rhs).
I'd call that a contradiction in terms.
Jan 24 '07 #8
lovecreatesbea...@gmail.com wrote:
Is it correct and safe to compare a string object with "", a pair of
quotation marks quoted empty string? If the string object: s = ""; does
s contain a single '\0'? Is it better to use std::string::size or
std::string::empty to deal with this condition?
You must realize that once you have a std::string, the null char has
no special meaning.

However, the std::string member functions that accept parameters of
char* (and no explicit length) effectively do what "strlen" does to
compute the length. Hence "" in the context of these parameters
is zero sized NOT an array of one char of '\0'.
Jan 24 '07 #9
Roland Pibinger wrote:
On Wed, 24 Jan 2007 11:14:46 +0100, Michael DOUBEZ wrote:
>But then, what about section 21.3.8.2:

template<class charT, class traits, class Allocator>
bool operator==(const basic_string<charT,traits,Allocator>& lhs,
const charT* rhs);

3 Returns:lhs == basic_string<charT,traits,Allocator>(rhs).

I'd call that a contradiction in terms.
Sort of, but not really.

The "Returns:" element of the description prescribes the value returned by
the function/operator, not how the value has to be computed.

An implementor can note that basic_string::compare() is overloaded for const
charT*, and use

lhs.compare(rhs) == 0

instead.
Bo Persson
Jan 24 '07 #10
On Wed, 24 Jan 2007 18:44:26 +0100, "Bo Persson" <bo*@gmb.dkwrote:
>Roland Pibinger wrote:
>On Wed, 24 Jan 2007 11:14:46 +0100, Michael DOUBEZ wrote:
>>But then, what about section 21.3.8.2:
template<class charT, class traits, class Allocator>
bool operator==(const basic_string<charT,traits,Allocator>& lhs,
const charT* rhs);
3 Returns:lhs == basic_string<charT,traits,Allocator>(rhs).

I'd call that a contradiction in terms.

Sort of, but not really.

The "Returns:" element of the description prescribes the value returned by
the function/operator, not how the value has to be computed.
Seems to be a plausible interpretation since the C++ Standard (AFAIK)
never prescribes a certain library implementation.

Best regards,
Roland Pibinger
Jan 24 '07 #11

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

Similar topics

3
by: Matt | last post by:
if (123 > 33) will return true and if ("123" > 33) will return true So my question is, if the above behaviors are the same?? If string is a number, and compare with another number, it will...
32
by: Indrid Colt | last post by:
Thank's for your help ! Indrid
9
by: Fei Liu | last post by:
In Accellerated C++, the author recommends that in a header file one should not declare using std::string, using std::vector etc instead one should directly specify the namespace specifier in...
1
by: dhtmlkitchen | last post by:
Why does "foo" instanceof String return false? It just seems counterintuitive to me. I mean, sure, I can always check the constructor: var fooVar = "foo"; var isString =...
7
pureenhanoi
by: pureenhanoi | last post by:
Hi bro! Can anyone tell me, how to compare two strings like Operating System compare file names. Example: if i have two string "T*.DOC" and "TIET1.DOC". The comparision operator must return TRUE...
21
by: Sami | last post by:
string = "" or string = string.Empty? should is the proper way? Sami
4
by: ndoe | last post by:
how to compare string in file i mean compare that string content digit or alphabet,and 1 question more can we count that line if first line variable = 1,if read second line variable = 2 and so on...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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,...
0
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...

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.