在 C++ 中按字母顺序对字符串排序
本文将演示如何在 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;
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn