473,288 Members | 1,750 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,288 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 9003


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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.