如何构造元组向量并像配对一样对它们进行排序?
How to Construct a vector of Tuples and Sort them like Pair?
假设,我有几个这样的整数元素:
(3 9 1), (1 5 2), (2 8 3), (1 4 4), (1 6 5), (1 5 6)
现在我想对元素进行排序,例如对向量进行排序。唯一的区别是我们这里有 3 个键,而不是 2 个键。排序后,元素将如下所示:
(1 4 4), (1 5 2), (1 5 6), (1 6 5), (2 8 3), (3 9 1)
是否有任何STL或其他技术可以实现此目的?我发现了Tuples
但有一些问题要很好地理解这一点。
你们能帮我什么吗?可以通过提供有用的链接或解释过程。
如果需要,可以只使用 STL 对tuple
的vector
进行排序。
#include <vector>
#include <tuple>
#include <iostream>
#include <algorithm>
int main(int argc, char * argv[]){
std::vector< std::tuple<int, int, int> > myVec;
myVec.emplace_back(3, 9, 1);
myVec.emplace_back(1, 5, 2);
myVec.emplace_back(2, 8, 3);
myVec.emplace_back(1, 4, 4);
myVec.emplace_back(1, 6, 5);
myVec.emplace_back(1, 5, 6);
std::sort(myVec.begin(), myVec.end());
for (auto i : myVec){
std::cout << std::get<0>(i) << ", " << std::get<1>(i) << ", " << std::get<2>(i) << 'n';
}
return 0;
}
这是此处的示例,刚刚使用您的值进行了修改。
它的工作原理是用emplace_back
构造一个新的tuple
并添加到向量的末尾。 如果需要,您可以使用push_back(std::make_tuple(...
但这似乎过于复杂。 然后,您可以像sort
任何其他vector
一样vector
。sort
的默认行为是升序。 您可以通过添加自己的comp
来更改sort
的行为。 比较函数的参数将为 2tuple
秒。 返回类型是比较的布尔结果。 由于tuple
已经有了所有的比较(<
、>
、<=
等(,所以你不需要重新定义它们。 你也可以用它来比较不同的东西,它只会变得更加复杂。
bool myFunction(const std::tuple<int, int, int> &i, const std::tuple<int, int, int> &j) {
return i > j;
}
....
std::sort(myVec.begin(), myVec.end(), myFunction);
....
这将按降序对向量进行排序。 您也可以将myFunction
替换为 lambda。
std::sort(myVec.begin(), myVec.end(), [](const std::tuple<int, int, int> &i, const std::tuple<int, int, int> &j) {
return i > j;
});
我提出了两种解决方案:第一种使用自定义结构,使用自定义<
运算符,该运算符使用std::tie
按顺序比较三个整数,第二种使用std::tuple
。
#include <iostream>
#include <set>
#include <vector>
struct three_integers {
int a, b, c;
bool operator<(const three_integers &other) const {
return std::tie(a, b, c) < std::tie(other.a, other.b, other.c);
}
};
int main(int argc, char *argv[]) {
// 1st solution using a custom structure
// std::set containers are always ordered according to the < operator
std::set<three_integers> sorted_set = {{3, 9, 1}, {1, 5, 2}, {2, 8, 3},
{1, 4, 4}, {1, 6, 5}, {1, 5, 6}};
std::cout << "Sorted set:n";
for (auto &element : sorted_set) {
std::cout << "(" << element.a << " " << element.b << " " << element.c << ")"
<< std::endl;
}
std::vector<three_integers> sorted_vector = {{3, 9, 1}, {1, 5, 2}, {2, 8, 3},
{1, 4, 4}, {1, 6, 5}, {1, 5, 6}};
// std::vector is not ordered, so we call std::sort on it to make it just like
// std::set, it will use our custom < operator
std::sort(sorted_vector.begin(), sorted_vector.end());
std::cout << "Sorted vector:n";
for (auto &element : sorted_vector) {
std::cout << "(" << element.a << " " << element.b << " " << element.c << ")"
<< std::endl;
}
// 2nd solution using tuples
std::vector<std::tuple<int, int, int>> sorted_vector_tuple = {
{3, 9, 1}, {1, 5, 2}, {2, 8, 3}, {1, 4, 4}, {1, 6, 5}, {1, 5, 6}};
std::sort(sorted_vector_tuple.begin(), sorted_vector_tuple.end());
std::cout << "Sorted vector of tuples:n";
for (auto &element : sorted_vector_tuple) {
std::cout << "(" << std::get<0>(element) << " " << std::get<1>(element)
<< " " << std::get<2>(element) << ")" << std::endl;
}
return 0;
}
输出
Sorted set:
(1 4 4)
(1 5 2)
(1 5 6)
(1 6 5)
(2 8 3)
(3 9 1)
Sorted vector:
(1 4 4)
(1 5 2)
(1 5 6)
(1 6 5)
(2 8 3)
(3 9 1)
Sorted vector of tuples:
(1 4 4)
(1 5 2)
(1 5 6)
(1 6 5)
(2 8 3)
(3 9 1)
我建议您阅读std::sort
文档。
- 二叉排序树无法编译
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- C++选择排序算法中的逻辑错误
- 使用C++程序合并排序没有得到正确的输出
- 计算排序向量的向量中唯一值的计数
- 排序算法c++
- 使用2个键的cpp-stl::优先级队列排序不正确
- 将结构向量排序为子组
- 在c++中尝试对对象数组进行排序时,出现std:bad_alloc错误
- 如何对点云数据进行排序
- 对字符串进行排序时,在c++中处理sort()
- 是否有类似std::lower_bound的函数,而不需要排序/分区输入
- 下面是排序算法O(n)吗
- std::sort()函数无法对向量的一部分进行排序
- shell排序中的交换和比较
- clang格式:禁用排序包含
- 显示错误输出的简单数组排序程序
- 如何构造元组向量并像配对一样对它们进行排序?
- <string> 设置像数字一样的排序
- 排序32位整型和排序64位整型一样快