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()); 10 8933
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());
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
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
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
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
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
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.
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'.
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
by: Indrid Colt |
last post by:
Thank's for your help !
Indrid
|
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...
|
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 =...
|
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...
|
by: Sami |
last post by:
string = "" or string = string.Empty?
should is the proper way?
Sami
|
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...
|
by: erikbower65 |
last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps:
1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal.
2. Connect to...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: kcodez |
last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: lllomh |
last post by:
How does React native implement an English player?
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
| |