繁体   English   中英

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

[英]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对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(...如果你想,但似乎过于复杂了。然后你sortvector ,就像任何其他的vector 。的默认行为sort是升序。你可以改变的行为sort由添加你自己的comp 。比较函数的参数将是2 tuple 。返回类型是你比较的布尔结果。因为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);
....

这将按降序对矢量进行排序。 你也可以用lambda替换myFunction

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文档。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM