【问题标题】:How to Construct a vector of Tuples and Sort them like Pair?如何构造元组向量并像对一样对它们进行排序?
【发布时间】:2018-04-30 17:48:44
【问题描述】:

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

(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,但在理解这一点时遇到了一些问题。
你们能以任何方式帮助我吗?可以通过提供有用的链接或解释流程来实现。

【问题讨论】:

标签: c++ sorting tuples


【解决方案1】:

如果您愿意,可以只使用 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;
}

这是来自here 的示例,刚刚使用您的值进行了修改。

它的工作原理是使用emplace_back 构造一个新的tuple,并将其添加到向量的末尾。如果需要,您可以使用push_back(std::make_tuple(...,但这似乎过于复杂。然后你 sort vector 就像你对任何其他 vector 一样。 sort 的默认行为是升序。您可以通过添加您自己的comp 来更改sort 的行为。比较函数的参数将是 2 tuples。返回类型是比较的布尔结果。由于tuples 已经拥有所有比较(&lt;&gt;&lt;= 等),因此您无需重新定义它们。你也可以用它来比较不同的东西,只是变得更复杂了。

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;
});

【讨论】:

    【解决方案2】:

    我提出了两种解决方案:第一种使用自定义结构,使用自定义&lt; 运算符,该运算符使用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 文档。

    【讨论】:

      猜你喜欢
      • 2011-12-30
      • 2021-01-05
      • 2010-10-22
      • 2013-04-23
      • 2013-11-08
      • 2021-09-21
      • 1970-01-01
      • 2019-09-28
      • 2022-06-13
      相关资源
      最近更新 更多