比较字符串,C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24957965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Comparing strings, c++
提问by Medvednic
I have a question:
我有个问题:
Let's say there are two std::string
s and I want to compare them, there is the option of using the compare()
function of the string
class but I also noticed that it is possible using simple < > !=
operators (both of the cases are possible even if I don't include the <string>
library).
Can someone explain why the compare()
function exists if a comparison can be made using simple operators?
假设有两个std::string
s 并且我想比较它们,可以选择使用类的compare()
函数,string
但我也注意到可以使用简单的< > !=
运算符(即使我不包括,这两种情况也是可能的)<string>
图书馆)。compare()
如果可以使用简单的运算符进行比较,有人可以解释为什么该函数存在吗?
btw I use Code::Blocks 13.12 here is an example of my code:
顺便说一句,我使用 Code::Blocks 13.12,这是我的代码示例:
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::getline;
int main()
{
string temp1, temp2;
cout << "Enter first word: ";
getline (cin,temp1);
cout << "Enter second word: ";
getline (cin,temp2);
cout << "First word: " << temp1 << endl << "Second word: " << temp2 << endl;
if (temp1 > temp2)
{
cout << "One" << endl;
}
if (temp1.compare(temp2) < 0)
{
cout << "Two" << endl;
}
return 0;
}
回答by Tom Fenech
.compare()
returns an integer, which is a measure of the difference between the two strings.
.compare()
返回一个整数,它是两个字符串之间差异的度量。
- A return value of
0
indicates that the two strings compare as equal. - A positive value means that the compared string is longer, or the first non-matching character is greater.
- A negative value means that the compared string is shorter, or the first non-matching character is lower.
- 的返回值
0
表示两个字符串比较相等。 - 正值意味着比较的字符串更长,或者第一个不匹配的字符更大。
- 负值意味着比较的字符串更短,或者第一个不匹配的字符更低。
operator==
simply returns a boolean, indicating whether the strings are equal or not.
operator==
简单地返回一个布尔值,指示字符串是否相等。
If you don't need the extra detail, you may as well just use ==
.
如果您不需要额外的细节,您也可以使用==
.
回答by Idz
string cat = "cat";
string human = "human";
cout << cat.compare(human) << endl;
This code will give -1 as a result. This is due to the first non-matching character of the compared string 'h' is lower or appears after 'c' in alphabetical order, even though the compared string, 'human' is longer than 'cat'.
此代码将给出 -1 作为结果。这是由于比较字符串 'h' 的第一个非匹配字符低于或按字母顺序出现在 'c' 之后,即使比较字符串 'human' 比 'cat' 长。
I find the return value described in cplusplus.comis more accurate which are-:
我发现cplusplus.com 中描述的返回值更准确,它们是-:
0 : They compare equal
<0 : Either the value of the first character that does not match is lower in the compared string, or all compared characters match but the compared string is shorter.
more than 0 : Either the value of the first character that does not match is greater in the compared string, or all compared characters match but the compared string is longer.
0 : 它们比较相等
<0 : 要么是比较字符串中第一个不匹配的字符的值较小,要么所有比较字符都匹配但比较字符串较短。
大于 0 :要么比较字符串中第一个不匹配的字符的值越大,要么所有比较字符都匹配但比较字符串较长。
Moreover, IMO cppreference.com's description is simpler and so far best describe to my own experience.
此外,IMO cppreference.com的描述更简单,到目前为止,我自己的经验是最好的描述。
negative value if
*this
appears before the character sequence specified by the arguments, in lexicographical orderzero if both character sequences compare equivalent
positive value if
*this
appears after the character sequence specified by the arguments, in lexicographical order
如果
*this
按字典顺序出现在参数指定的字符序列之前,则为负值如果两个字符序列比较相等,则为零
如果
*this
按字典顺序出现在参数指定的字符序列之后,则为正值
回答by Cheers and hth. - Alf
Regarding the question,
关于这个问题,
”can someone explain why the
compare()
function exists if a comparison can be made using simple operands?
”
compare()
如果可以使用简单的操作数进行比较,有人可以解释为什么存在该函数吗?
Relative to <
and ==
, the compare
function is conceptually simpler and in practice it canbe more efficient since it avoids two comparisons per item for ordinary ordering of items.
相对于<
and ==
,该compare
函数在概念上更简单,实际上它可以更有效,因为它避免了对项目的普通排序对每个项目进行两次比较。
As an example of simplicity, for small integer values you can write a compare function like this:
举个简单的例子,对于小的整数值,你可以写一个像这样的比较函数:
auto compare( int a, int b ) -> int { return a - b; }
which is highly efficient.
这是高效的。
Now for a structure
现在是一个结构
struct Foo
{
int a;
int b;
int c;
};
auto compare( Foo const& x, Foo const& y )
-> int
{
if( int const r = compare( x.a, y.a ) ) { return r; }
if( int const r = compare( x.b, y.b ) ) { return r; }
return compare( x.c, y.c );
}
Trying to express this lexicographic compare directly in terms of <
you wind up with horrendous complexity and inefficiency, relatively speaking.
<
相对而言,试图直接根据您的术语来表达这种字典顺序比较会导致可怕的复杂性和低效率。
With C++11, for the simplicity alone ordinary less-than comparison based lexicographic compare can be very simply implemented in terms of tuple comparison.
使用 C++11,为了简单起见,基于普通小于比较的字典比较可以非常简单地在元组比较方面实现。