#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
int main() {
// 创建一个字符串向量
vector<string> words = {"banana", "apple", "orange", "grape", "cherry"};
// 使用sort函数对字符串向量进行排序
sort(words.begin(), words.end());
// 输出排序后的结果
cout << "排序后的字符串:" << endl;
for (const auto& word : words) {
cout << word << endl;
}
return 0;
}
#include <iostream>、#include <vector>、#include <algorithm> 和 #include <string> 分别用于输入输出流、向量容器、算法库和字符串处理。using namespace std; 简化了标准库的调用,避免每次使用标准库元素时都要加上 std:: 前缀。vector<string> words 定义了一个字符串类型的向量,并初始化了一些水果名称。sort(words.begin(), words.end()); 使用 C++ 标准库中的 sort 函数对向量中的字符串进行字典序排序。for 循环遍历排序后的向量,并逐个输出每个字符串。这样可以实现对一组字符串的简单排序。
上一篇:c++swap函数
下一篇:c++ else if
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站