vector按自定义方法排序
时间: 2023-09-29 08:08:32 浏览: 60
可以使用C++中的sort函数,并自定义比较方法。
假设我们有一个vector<int> v,现在我们要按照以下规则排序:
1. 奇数排在偶数前面,奇数从小到大排序,偶数从大到小排序。
2. 如果两个数的奇偶性相同,则按照从小到大排序。
我们可以使用一个自定义的比较函数来实现这个排序规则:
```cpp
bool myCompare(int a, int b) {
if (a % 2 == 1 && b % 2 == 0) { // a是奇数,b是偶数
return true;
}
if (a % 2 == 0 && b % 2 == 1) { // a是偶数,b是奇数
return false;
}
if (a % 2 == 1 && b % 2 == 1) { // a和b都是奇数
return a < b;
}
return a > b; // a和b都是偶数
}
```
这个函数接受两个参数a和b,返回一个bool值表示a是否应该排在b的前面。根据上面的排序规则,我们可以用if-else语句来进行判断。
然后,我们可以使用sort函数来按照这个比较函数来排序:
```cpp
sort(v.begin(), v.end(), myCompare);
```
这样,v就按照我们自定义的方法排序了。
相关问题
vector 自定义排序
在C++中,我们可以使用lambda表达式来实现自定义排序。首先,我们需要定义一个vector对象并填充它的元素。然后,使用sort函数对vector进行排序。在sort函数的第三个参数中,我们可以使用lambda表达式来指定排序的方式。在lambda表达式中,我们可以自定义比较函数来决定元素的顺序。比如,如果我们想要按照元素的第二个值进行降序排序,我们可以使用以下代码:
```cpp
vector<vector<int>> vec(5, vector<int>(5)); // 定义一个5x5的二维vector
sort(vec.begin(), vec.end(), [](const vector<int>& a, const vector<int>& b){
return a > b<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [vector排序|vector多维数组排序|vector自定义排序|不改变相同元素相对顺序比较](https://blog.csdn.net/qq_39898780/article/details/121349269)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [【C++】vector数组排序](https://blog.csdn.net/weixin_46308081/article/details/117195502)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
vector的sort自定义排序
要对一个 vector 进行自定义排序,可以使用 `std::sort` 函数并提供一个自定义的比较函数作为第三个参数。比较函数应该接受两个参数,并返回一个布尔值,用于指示两个元素的顺序关系。
下面是一个示例,展示如何对一个存储了自定义结构体的 vector 进行排序:
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
struct MyStruct {
int id;
std::string name;
};
bool compareById(const MyStruct& a, const MyStruct& b) {
return a.id < b.id;
}
int main() {
std::vector<MyStruct> structs = {
{3, "Charlie"},
{1, "Alice"},
{2, "Bob"}
};
std::sort(structs.begin(), structs.end(), compareById);
for (const auto& s : structs) {
std::cout << s.id << ": " << s.name << std::endl;
}
return 0;
}
```
在上面的代码中,`compareById` 函数用于比较两个 `MyStruct` 结构体对象的 `id` 字段。通过将这个函数作为第三个参数传递给 `std::sort`,我们可以根据 `id` 字段对结构体进行排序。
输出将是:
```
1: Alice
2: Bob
3: Charlie
```
你可以根据自己的需求编写不同的比较函数来实现不同的排序方式。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.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)