我刚读了一些关于使用的建议
std::string s = get_string();
std::string t = another_string();
if( !s.compare(t) )
{
代替
if( s == t )
{
我几乎总是使用最后一个,因为我已经习惯了它,它感觉自然,更具可读性。 我甚至不知道有一个单独的比较功能。 更确切地说,我认为==会调用compare()。
有什么区别? 在哪种情况下,一种方式应该受到另一种方式的青睐?
我只考虑我需要知道字符串是否与另一个字符串相同的情况。
#1楼
这里没有涉及的一件事是,它取决于我们将字符串与c字符串,c字符串与字符串或字符串与字符串进行比较。
一个主要的区别是,为了比较两个字符串,在进行比较之前检查大小相等,这使得==运算符比比较更快。
这是我在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
inline bool
operator==(const basic_string<_chart _traits _alloc>& __lhs,
const basic_string<_chart _traits _alloc>& __rhs)
{ return __lhs.compare(__rhs) == 0; }
template
inline
typename __gnu_cxx::__enable_if<__is_char>::__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
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
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环境中编译并运行代码,则两者都非常相似。
仅供参考,为了得出这样的结论,我进行了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==,std::allocator > (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,std::allocator >::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,std::allocator >::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,std::allocator >::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都是无关紧要的)。