compare string and "" string literal

Discussion in 'C++' started by lovecreatesbea...@gmail.com, Jan 24, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. Guest

    Compare std::string object with "" literal (Re: compare string and "" string literal)

    On 1ÔÂ24ÈÕ, ÉÏÎç10ʱ41·Ö, ""
    <> 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 '\'? 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, 2007
    #2
    1. Advertising

  3. Re: Compare std::string object with "" literal (Re: compare stringand "" string literal)

    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
    Michael DOUBEZ, Jan 24, 2007
    #3
  4. Re: Compare std::string object with "" literal (Re: compare string and "" string literal)

    On Wed, 24 Jan 2007 08:53:19 +0100, Michael DOUBEZ wrote:
    > 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, Jan 24, 2007
    #4
  5. Re: Compare std::string object with "" literal (Re: compare stringand "" string literal)

    Roland Pibinger a écrit :
    > On Wed, 24 Jan 2007 08:53:19 +0100, Michael DOUBEZ wrote:
    >> 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::eek:perator==(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
    Michael DOUBEZ, Jan 24, 2007
    #5
  6. Re: Compare std::string object with "" literal (Re: compare string and "" string literal)

    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:
    >>> 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::eek:perator==(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, Jan 24, 2007
    #6
  7. Re: Compare std::string object with "" literal (Re: compare stringand "" string literal)

    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:
    >>>> 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::eek:perator==(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
    Michael DOUBEZ, Jan 24, 2007
    #7
  8. Re: Compare std::string object with "" literal (Re: compare string and "" string literal)

    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.
    Roland Pibinger, Jan 24, 2007
    #8
  9. Ron Natalie Guest

    Re: Compare std::string object with "" literal (Re: compare stringand "" string literal)

    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'.
    Ron Natalie, Jan 24, 2007
    #9
  10. Bo Persson Guest

    Re: Compare std::string object with "" literal (Re: compare string and "" string literal)

    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
    Bo Persson, Jan 24, 2007
    #10
  11. Re: Compare std::string object with "" literal (Re: compare string and "" string literal)

    On Wed, 24 Jan 2007 18:44:26 +0100, "Bo Persson" <> wrote:
    >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
    Roland Pibinger, Jan 24, 2007
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Prakash Prabhu

    String literal and String Object

    Prakash Prabhu, Aug 27, 2003, in forum: Java
    Replies:
    3
    Views:
    635
    John C. Bollinger
    Aug 27, 2003
  2. Replies:
    5
    Views:
    735
    Frank Schmitt
    Sep 22, 2003
  3. herrcho
    Replies:
    7
    Views:
    421
    Jack Klein
    Sep 26, 2003
  4. al

    String literal and const

    al, Dec 19, 2003, in forum: C Programming
    Replies:
    7
    Views:
    439
    Ben Pfaff
    Dec 20, 2003
  5. Anonieko Ramos

    What's wrong with rpc-literal? Why use doc-literal?

    Anonieko Ramos, Sep 27, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    374
    Anonieko Ramos
    Sep 27, 2004
Loading...

Share This Page