迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > C++ >

在 C++ 中按字母顺序对字符串排序

作者:迹忆客 最近更新:2023/03/15 浏览次数:

本文将演示如何在 C++ 中按字母顺序对字符串排序的多种方法。

使用 std::sort 算法在 C++ 中按字母顺序对字符串进行排序

std::sort 是 STL 算法库的一部分,它为基于范围的结构实现了通用的排序方法。该函数通常使用 operator< 对给定序列进行排序;因此,可以使用此默认行为按字母顺序对字符串对象进行排序。std::sort 只接受范围说明符-第一个和最后一个元素迭代器。请注意,不能保证保留相等元素的顺序。

在下面的示例中,我们演示了基本方案,其中对字符串的向量进行了排序并打印到了 cout 流中。

#include <iostream>
#include <string>
#include <vector>

using std::cout; using std::sort;
using std::vector; using std::string;
using std::endl;

int main() {
    vector<string> arr = { "raid", "implementation", "states", "all",
                           "the", "requirements", "parameter", "a",
                           "and", "or", "execution", "participate" };


    for (const auto &item : arr) {
        cout << item << "; ";
    }
    cout << endl;

    sort(arr.begin(), arr.end());

    for (const auto &item : arr) {
        cout << item << "; ";
    }
    cout << endl;

    exit(EXIT_SUCCESS);
}

输出:

raid; implementation; states; all; the; requirements; parameter; a; and; or; execution; participate;
a; all; and; execution; implementation; or; parameter; participate; raid; requirements; states; the;

另外,我们可以在 std::sort 方法中使用可选的第三个参数来指定确切的比较函数。在这种情况下,我们重新实现了前面的代码示例,以比较字符串中的最后一个字母并对其进行相应的排序。注意比较函数应该有两个参数,并返回一个 bool 值。下一个示例使用 lambda 表达式作为比较函数,并利用 back 内置函数检索字符串的最后一个字母。

#include <iostream>
#include <string>
#include <vector>

using std::cout; using std::sort;
using std::vector; using std::string;
using std::endl;

int main() {
    vector<string> arr = { "raid", "implementation", "states", "all",
                           "the", "requirements", "parameter", "a",
                           "and", "or", "execution", "participate" };


    for (const auto &item : arr) {
        cout << item << "; ";
    }
    cout << endl;

    sort(arr.begin(), arr.end(),
         [] (string &s1, string &s2) { return s1.back() < s2.back(); });

    for (const auto &item : arr) {
        cout << item << "; ";
    }
    cout << endl;

    exit(EXIT_SUCCESS);
}

输出:

raid; implementation; states; all; the; requirements; parameter; a; and; or; execution; participate;
a; raid; and; the; participate; all; implementation; execution; parameter; or; states; requirements;

使用 std::sort 算法在 C++ 中按长度对字符串排序

对字符串向量进行排序的另一种有用情况是按其长度排序。我们将使用与前面的示例代码相同的 lambda 函数结构,只是将 back 方法更改为 size。不过请注意,该比较函数一定不能修改传递给它的对象。

#include <iostream>
#include <string>
#include <vector>

using std::cout; using std::sort;
using std::vector; using std::string;
using std::endl;

int main() {
    vector<string> arr = { "raid", "implementation", "states", "all",
                           "the", "requirements", "parameter", "a",
                           "and", "or", "execution", "participate" };


    for (const auto &item : arr) {
        cout << item << "; ";
    }
    cout << endl;

    sort(arr.begin(), arr.end(),
             [] (string &s1, string &s2) { return s1.size() < s2.size(); });

    for (const auto &item : arr) {
        cout << item << "; ";
    }
    cout << endl;

    exit(EXIT_SUCCESS);
}

输出:

raid; implementation; states; all; the; requirements; parameter; a; and; or; execution; participate;
a; or; all; the; and; raid; states; parameter; execution; participate; requirements; implementation;

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Arduino 中停止循环

发布时间:2024/03/13 浏览次数:444 分类:C++

可以使用 exit(0),无限循环和 Sleep_n0m1 库在 Arduino 中停止循环。

Arduino 复位

发布时间:2024/03/13 浏览次数:315 分类:C++

可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。

Arduino 的字符转换为整型

发布时间:2024/03/13 浏览次数:181 分类:C++

可以使用简单的方法 toInt()函数和 Serial.parseInt()函数将 char 转换为 int。

Arduino 串口打印多个变量

发布时间:2024/03/13 浏览次数:381 分类:C++

可以使用 Serial.print()和 Serial.println()函数在串口监视器上显示变量值。

Arduino if 语句

发布时间:2024/03/13 浏览次数:123 分类:C++

可以使用 if 语句检查 Arduino 中的不同条件。

Arduino ICSP

发布时间:2024/03/13 浏览次数:214 分类:C++

ICSP 引脚用于两个 Arduino 之间的通信以及对 Arduino 引导加载程序进行编程。

使用 C++ 编程 Arduino

发布时间:2024/03/13 浏览次数:127 分类:C++

本教程将讨论使用 Arduino IDE 在 C++ 中对 Arduino 进行编程。

Arduino 中的子程序

发布时间:2024/03/13 浏览次数:168 分类:C++

可以通过在 Arduino 中声明函数来处理子程序。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便