如何构造元组向量并像配对一样对它们进行排序?

How to Construct a vector of Tuples and Sort them like Pair?

本文关键字:一样 排序 向量 元组 何构造      更新时间:2023-10-16

假设,我有几个这样的整数元素:

(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 对tuplevector进行排序。

#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一样vectorsort的默认行为是升序。 您可以通过添加自己的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文档。