按字典顺序比较字符串

问题描述 投票:0回答:4

我想如果我使用“">”和“<" in c++ to compare strings, these would compare them lexicographically, the problem is that this only works sometimes in my computer. For example

等运算符
if("aa" > "bz") cout<<"Yes";

这不会打印任何内容,这就是我需要的,但是如果我输入

if("aa" > "bzaa") cout<<"Yes";

这会打印“Yes”,为什么会发生这种情况?或者我应该使用其他方法按字典顺序比较字符串?

c++ string compare
4个回答
40
投票

比较

std::string
- 这样工作。但是您正在比较字符串文字。要进行比较,您需要用它们初始化 std::string 或使用 strcmp:

if(std::string("aa") > std::string("bz")) cout<<"Yes";

这是 C++ 风格的解决方案。

或者:

if(strcmp("aa", "bz") > 0) cout<<"Yes";

编辑(感谢康拉德·鲁道夫的评论):事实上,在第一个版本中,只有一个操作数应该显式转换,因此:

if(std::string("aa") > "bz") cout<<"Yes";

将再次按预期工作。

编辑(感谢 churill 的评论):从 c++14 开始,你可以使用字符串文字:

if("aa"s > "bz") cout<<"Yes";

7
投票

您正在比较“原始”字符串,其类型为

char const *

以下内容本质上等同于您的示例:

char const * s1 = "aa";
char const * s2 = "bz";
if ( s1 > s2 ) cout<<"Yes";

这是比较指针(字符串的内存地址),而不是内容。

@izomorphius 提出了一些很好的解决方案。


0
投票

您可以使用

strcmp()
头文件中包含的
#include <cstring>
函数。
strcmp()
逐个字符地比较两个字符串。该过程将继续,直到达到
NULL
或其中一个字符串变得不相等(字符变得不相等)。 例如:

#include <iostream>
#include <cstring>


void display(char *lhs, char *rhs, int result)
{
    if(result > 0)
        std::cout << rhs << " precedes " << lhs << std::endl;
    else if (result < 0)
        std::cout << lhs << " precedes " << rhs << std::endl;
    else
        std::cout << lhs << " and " << rhs << " are same" << std::endl;
}

int main()
{
    char lhs[] = "aa";
    char rhs[] = "bz";
    int result;

    result = strcmp(lhs,rhs);
    display(lhs,rhs,result);

    result = strcmp(lhs,lhs);
    display(lhs,lhs,result);

    return 0;
}

输出:

aa precedes bz
aa and aa are same

0
投票

C++20

operator<=>
(宇宙飞船操作员)可以让你一次性获得
<
==
>

这是 Google 员工感兴趣的另一个相关功能:

主.cpp

#include <cassert>
#include <string>

int main() {
    std::string aa = "aa", ab = "ab";
    assert(((aa <=> aa) == 0));
    assert(((aa <=> ab)  < 0));
    assert(((ab <=> aa)  > 0));
}

编译并运行:

g++ -ggdb3 -O0 -std=c++20 -Wall -Wextra -pedantic -o main.out main.cpp
./main.out

请注意,在这种情况下,您只能将

<=>
(一个
std::strong_ordering
对象)的输出与
0
进行比较,而不能与其他任何东西进行比较。例如。如果你尝试:

((aa <=> aa) == 1)

if 失败并显示 119 行长的错误消息,我们非常喜欢从 C++ 开始:

main.cpp: In function ‘int main()’:                                                                                                                                                           main.cpp:6:25: error: no match for ‘operator==’ (operand types are ‘std::strong_ordering’ and ‘int’)
    6 |     assert(((aa <=> aa) == 1));                                                                                                                                                       
      |             ~~~~~~~~~~~ ^~ ~                                                                                                                                                          
      |                 |          |                                                                                                                                                          
      |                 |          int                                                                                                                                                        
      |                 std::strong_ordering                                                   
In file included from /usr/include/c++/13/string:43,                             
                 from main.cpp:2:

例如提到了这一点位于:https://en.cppreference.com/w/cpp/utility/compare/strong_ordering

尝试将

strong_ordering
与整数文字0 以外的任何内容进行比较的程序的行为是未定义的。

相关更广泛的问题:C++ 中的 <=>(“太空飞船”,三向比较)运算符是什么?

在 Ubuntu 24.04、GCC 13.2.0 上测试。

© www.soinside.com 2019 - 2024. All rights reserved.