C ++ string ==和compare()之间的区别?

我刚读了一些关于使用的建议 性能

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

if( !s.compare(t) ) 
{

代替 优化

if( s == t )
{

我几乎老是使用最后一个,由于我已经习惯了它,它感受天然,更具可读性。 我甚至不知道有一个单独的比较功能。 更确切地说,我认为==会调用compare()。 ui

有什么区别? 在哪一种状况下,一种方式应该受到另外一种方式的青睐? spa

我只考虑我须要知道字符串是否与另外一个字符串相同的状况。 调试


#1楼

这里没有涉及的一件事是,它取决于咱们将字符串与c字符串,c字符串与字符串或字符串与字符串进行比较。 code

一个主要的区别是,为了比较两个字符串,在进行比较以前检查大小相等,这使得==运算符比比较更快。 orm

这是我在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; }

#2楼

在内部,string :: operator ==()使用string :: compare()。 请参考: CPlusPlus - String :: Operator ==() 字符串

我写了一个小应用程序来比较性能,显然若是你在调试环境中编译和运行你的代码,String :: compare()比string :: operator ==()略快。 可是,若是您在Release环境中编译并运行代码,则二者都很是类似。 get

仅供参考,为了得出这样的结论,我进行了1,000,000次迭代。

为了在调试环境中证实为何string :: compare更快,我去了程序集,这里是代码:

DEBUG BUILD

字符串::运算符==()

if (str1 == str2)
00D42A34  lea         eax,[str2]  
00D42A37  push        eax  
00D42A38  lea         ecx,[str1]  
00D42A3B  push        ecx  
00D42A3C  call        std::operator==<char,std::char_traits<char>,std::allocator<char> > (0D23EECh)  
00D42A41  add         esp,8  
00D42A44  movzx       edx,al  
00D42A47  test        edx,edx  
00D42A49  je          Algorithm::PerformanceTest::stringComparison_usingEqualOperator1+0C4h (0D42A54h)

字符串::比较()

if (str1.compare(str2) == 0)
00D424D4  lea         eax,[str2]  
00D424D7  push        eax  
00D424D8  lea         ecx,[str1]  
00D424DB  call        std::basic_string<char,std::char_traits<char>,std::allocator<char> >::compare (0D23582h)  
00D424E0  test        eax,eax  
00D424E2  jne         Algorithm::PerformanceTest::stringComparison_usingCompare1+0BDh (0D424EDh)

你能够在string :: operator ==()中看到它必须执行额外的操做(添加esp,8和movzx edx,al)

发布

字符串::运算符==()

if (str1 == str2)
008533F0  cmp         dword ptr [ebp-14h],10h  
008533F4  lea         eax,[str2]  
008533F7  push        dword ptr [ebp-18h]  
008533FA  cmovae      eax,dword ptr [str2]  
008533FE  push        eax  
008533FF  push        dword ptr [ebp-30h]  
00853402  push        ecx  
00853403  lea         ecx,[str1]  
00853406  call        std::basic_string<char,std::char_traits<char>,std::allocator<char> >::compare (0853B80h)

字符串::比较()

if (str1.compare(str2) == 0)
    00853830  cmp         dword ptr [ebp-14h],10h  
    00853834  lea         eax,[str2]  
    00853837  push        dword ptr [ebp-18h]  
    0085383A  cmovae      eax,dword ptr [str2]  
    0085383E  push        eax  
    0085383F  push        dword ptr [ebp-30h]  
    00853842  push        ecx  
00853843  lea         ecx,[str1]  
00853846  call        std::basic_string<char,std::char_traits<char>,std::allocator<char> >::compare (0853B80h)

两个汇编代码都很是类似,由于编译器执行优化。

最后,在我看来,性能提高能够忽略不计,所以我真的会让开发人员决定哪个是首选的,由于二者都达到了相同的结果(特别是当它是发布版本时)。


#3楼

在Visual Studio 2012调试器中,检查字符串时,只有如下方法正常工做:

strcmp(somestring.c_str(),"")==0

返回true。

somestring.compare("")

返回1,和

somestring==""

return:no运算符“==”匹配这些操做数。

somestring.c_str()==""

return:发生了未指定的错误。


#4楼

假设考虑两个字符串s和t。
给他们一些价值。
当您使用(s == t)比较它们时,它返回一个布尔值(true或false,1或0)。
可是当使用s.compare(t)进行比较时,表达式返回一个值
(i) 0 - 若是s和t相等
(ii) <0 - 若是s中的第一个不匹配字符的值小于t的值,或者s的长度小于t的长度。
(iii) > 0 - 若是t中第一个不匹配字符的值小于s的值,或者t的长度小于s的长度。


#5楼

compare有比较子串的重载。 若是你要比较整个字符串,你应该只使用==运算符(不管是否调用compare都是可有可无的)。

相关文章
相关标签/搜索