我刚读了一些使用建议

std::string s = get_string();
std::string t = another_string();

if( !s.compare(t) ) 
{

而不是

if( s == t )
{

我几乎总是使用最后一个,因为我习惯了它,它感觉更自然,更可读。我甚至不知道还有一个单独的比较函数。 更准确地说,我认为==将调用compare()。

有什么不同?在哪些情况下,一种方式比另一种方式更受青睐?

我只考虑需要知道一个字符串是否与另一个字符串的值相同的情况。


当前回答

如果字符串相等,Compare()将返回false(好吧,0)。

所以,不要轻易地把一个换成另一个。

使用任何使代码更具可读性的方法。

其他回答

这里没有涉及到的一件事是,它取决于我们是比较string与c string, c string与string,还是string与string。

一个主要的区别是,比较两个字符串时,在进行比较之前检查大小是否相等,这使得==操作符比比较操作符更快。

这是比较,因为我看到它在g++ Debian 7

// operator ==
  /**
   *  @brief  Test equivalence of two strings.
   *  @param __lhs  First string.
   *  @param __rhs  Second string.
   *  @return  True if @a __lhs.compare(@a __rhs) == 0.  False otherwise.
   */
  template<typename _CharT, typename _Traits, typename _Alloc>
    inline bool
    operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
           const basic_string<_CharT, _Traits, _Alloc>& __rhs)
    { return __lhs.compare(__rhs) == 0; }

  template<typename _CharT>
    inline
    typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, bool>::__type
    operator==(const basic_string<_CharT>& __lhs,
           const basic_string<_CharT>& __rhs)
    { return (__lhs.size() == __rhs.size()
          && !std::char_traits<_CharT>::compare(__lhs.data(), __rhs.data(),
                            __lhs.size())); }

  /**
   *  @brief  Test equivalence of C string and string.
   *  @param __lhs  C string.
   *  @param __rhs  String.
   *  @return  True if @a __rhs.compare(@a __lhs) == 0.  False otherwise.
   */
  template<typename _CharT, typename _Traits, typename _Alloc>
    inline bool
    operator==(const _CharT* __lhs,
           const basic_string<_CharT, _Traits, _Alloc>& __rhs)
    { return __rhs.compare(__lhs) == 0; }

  /**
   *  @brief  Test equivalence of string and C string.
   *  @param __lhs  String.
   *  @param __rhs  C string.
   *  @return  True if @a __lhs.compare(@a __rhs) == 0.  False otherwise.
   */
  template<typename _CharT, typename _Traits, typename _Alloc>
    inline bool
    operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
           const _CharT* __rhs)
    { return __lhs.compare(__rhs) == 0; }

Suppose consider two string s and t. Give them some values. When you compare them using (s==t) it returns a boolean value(true or false , 1 or 0). But when you compare using s.compare(t) ,the expression returns a value (i) 0 - if s and t are equal (ii) <0 - either if the value of the first unmatched character in s is less than that of t or the length of s is less than that of t. (iii) >0 - either if the value of the first unmatched character in t is less than that of s or the length of t is less than that of s.

Compare具有用于比较子字符串的重载。如果你比较的是整个字符串,你应该使用==操作符(是否调用compare几乎无关紧要)。

Compare()等价于strcmp()。==是简单的相等检查。因此Compare()返回一个int型,==是一个布尔型。

如果只是想检查字符串是否相等,可以使用==运算符。确定两个字符串是否相等比查找排序(这是compare()给出的)更简单,因此在您的情况下,使用相等操作符可能性能更好。

更长的回答:API提供了一个检查字符串是否相等的方法和一个检查字符串顺序的方法。您需要字符串相等,因此使用相等操作符(以便您的期望与标准库实现者的期望一致)。如果性能很重要,那么您可能希望测试这两种方法并找出最快的方法。