简体   繁体   English

在CPP中制作矢量三元组的简便方法

[英]Easy way to make vector triplet in CPP

What would be an easy way to construct a vector triplet of ints in CPP? 在CPP中构建向量三元组的简单方法是什么?

ie instead of a pair of 2 ints , 即代替一对2个整数,

std::vector<std::pair<int, int> > vec;

I want 3 int's tied together as one element of the vector. 我想将3个int绑在一起作为向量的一个元素。

I realized one way would be to make 2 sub-nested pairs, but this method gets messy. 我意识到一种方法是制作2个子嵌套对,但这种方法会变得混乱。 I am not aware of all the details of CPP, therefore please recommend an easier way if available. 我不知道CPP的所有细节,因此请推荐一种更简单的方法(如果有的话)。 Thank you. 谢谢。

std::vector<std::tuple<int,int,int>> myvec;

No need to over-engineer. 无需过度工程。

struct Triplet
{
  int  one_, two_, three_;
};

vector<Triplet> triplets;

Check out boost tuple http://www.boost.org/doc/libs/1_49_0/libs/tuple/doc/tuple_users_guide.html 查看boost元组http://www.boost.org/doc/libs/1_49_0/libs/tuple/doc/tuple_users_guide.html

You can easily create Pairs, triples, quadruples, up to n-uples! 您可以轻松创建对,三元组,四元组,最多n-uples!

In C++11, there is std::array , see here . 在C ++ 11中,有std::array ,请参见此处 In C++03, I would probably define a struct of 3 int s and make a vector of those. 在C ++ 03中,我可能会定义一个3 int的结构并创建它们的向量。

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

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