473,748 Members | 2,328 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::si ze or
std::string::em pty to deal with this condition? Thank you.

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

Jan 24 '07 #1
10 9067


On 1ÔÂ24ÈÕ, ÉÏÎç10ʱ41·Ö, "lovecreatesbea ...@gmail.com"
<lovecreatesbea ...@gmail.comwr ote:
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::si ze or
std::string::em pty 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::si ze or
std::string::em pty 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::si ze or
std::string::em pty to deal with this condition?
Unless you have a specific reason to do otherwise, the
std::string::em pty 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::si ze 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::op erator==(const string& c1, const string& c2);

So encoutering:
s == "blah"

The compiler matched "blah" as a string through the relevant constructor:
std::string::st ring( 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:
>>lovecreatesbe a...@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::op erator==(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==(cons t basic_string<ch arT,traits,Allo cator>& lhs,
const basic_string<ch arT,traits,Allo cator>& rhs);

template<class charT, class traits, class Allocator>
bool operator==(cons t charT* lhs,
const basic_string<ch arT,traits,Allo cator>& rhs);

template<class charT, class traits, class Allocator>
bool operator==(cons t basic_string<ch arT,traits,Allo cator>& 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:
lovecreatesb ea...@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::op erator==(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==(cons t basic_string<ch arT,traits,Allo cator>& lhs,
const basic_string<ch arT,traits,Allo cator>& rhs);

template<class charT, class traits, class Allocator>
bool operator==(cons t charT* lhs,
const basic_string<ch arT,traits,Allo cator>& rhs);

template<class charT, class traits, class Allocator>
bool operator==(cons t basic_string<ch arT,traits,Allo cator>& 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==(cons t basic_string<ch arT,traits,Allo cator>& lhs,
const basic_string<ch arT,traits,Allo cator>& rhs);

1 Returns:lhs.com pare(rhs) == 0.

template<class charT, class traits, class Allocator>
bool operator==(cons t charT* lhs,
const basic_string<ch arT,traits,Allo cator>& rhs);

2 Returns:basic_s tring<charT,tra its,Allocator>( lhs) == rhs.

template<class charT, class traits, class Allocator>
bool operator==(cons t basic_string<ch arT,traits,Allo cator>& lhs,
const charT* rhs);

3 Returns:lhs == basic_string<ch arT,traits,Allo cator>(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<cla ss charT, class traits, class Allocator>
bool operator==(cons t basic_string<ch arT,traits,Allo cator>& lhs,
const charT* rhs);

3 Returns:lhs == basic_string<ch arT,traits,Allo cator>(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::si ze or
std::string::em pty 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<cla ss charT, class traits, class Allocator>
bool operator==(cons t basic_string<ch arT,traits,Allo cator>& lhs,
const charT* rhs);

3 Returns:lhs == basic_string<ch arT,traits,Allo cator>(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::c ompare() is overloaded for const
charT*, and use

lhs.compare(rhs ) == 0

instead.
Bo Persson
Jan 24 '07 #10

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

Similar topics

3
21028
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 be the same behavior as compare 2 numbers?
32
2691
by: Indrid Colt | last post by:
Thank's for your help ! Indrid
9
3783
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 code. for example, this is bad practice: header.h #include <string> using std::string;
1
2295
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 = fooVar.constructor == String;
7
2714
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 values when compare them
21
2973
by: Sami | last post by:
string = "" or string = string.Empty? should is the proper way? Sami
4
3021
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 thanks e.g i have file with name data.txt with file like this 123456
0
8822
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
9359
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
9236
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...
1
6792
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
6072
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
4592
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...
1
3298
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2206
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.