字符串的排序C++
时间: 2023-08-06 20:11:24 浏览: 42
可以使用C++中自带的sort函数来对字符串数组进行排序。
示例代码如下:
```cpp
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main() {
string arr[] = {"orange", "apple", "banana", "grape", "pear"};
int n = sizeof(arr) / sizeof(arr[0]);
// 对字符串数组进行排序
sort(arr, arr + n);
// 输出排序后的结果
for(int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
return 0;
}
```
输出结果为:
```
apple banana grape orange pear
```
这里使用了C++中的string类型,string类型也可以使用sort函数进行排序。
相关问题
string类的字符串排序c++
可以使用`sort`函数对字符串数组排序,示例代码如下:
```c++
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main() {
string arr[] = {"apple", "banana", "orange", "grape"};
int n = sizeof(arr) / sizeof(arr[0]);
sort(arr, arr + n);
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
```
上述代码中,我们定义了一个字符串数组`arr`,然后使用`sort`函数对其进行排序。最后遍历数组并输出排序结果。需要注意的是,我们在`sort`函数中传入的第二个参数是指向数组末尾的迭代器,而不是数组长度。
C++不等长字符串排序
在C++中,标准库并没有提供直接支持不等长字符串排序的函数,因为C++的`std::string`类型默认是等长的,即所有的字符数组都有相同的长度。如果你需要对不等长字符串进行排序,通常会用到自定义比较函数或者使用一些第三方库,如`boost`中的`multi_index_container`。
一个常见的做法是定义一个结构体或类,其中包含一个字符串和其长度,然后使用这个复合类型的容器来进行排序。这里是一个简单的例子:
```cpp
#include <string>
#include <vector>
#include <algorithm>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/hashed_index.hpp>
// 定义一个结构体,包含字符串和长度
struct StringWithLength {
std::string str;
size_t length;
};
// 自定义比较函数,用于按长度排序
bool compareStringLength(const StringWithLength& a, const StringWithLength& b) {
return a.length < b.length;
}
int main() {
std::vector<StringWithLength> strings = {/*你的字符串列表*/};
// 使用boost的multi_index_container进行排序
boost::multi_index_container<StringWithLength,
boost::multi_index::indexed_by<boost::multi_index::ordered_unique<boost::multi_index::member<StringWithLength, size_t, &StringWithLength::length>>,
boost::multi_index::hashed_unique<boost::multi_index::tag<>, boost::multi_index::member<StringWithLength, std::string const&, &StringWithLength::str>>>>
my_string_list(strings);
std::sort(my_string_list.begin(), my_string_list.end(), compareStringLength);
// 现在我的_string_list已经是按长度升序排列的了
for (const auto& str : my_string_list) {
std::cout << str.str << " (" << str.length << ")\n";
}
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)