Easy way to make vector triple in CPP

What would be a simple way to build an int vector triplet in CPP?

ie instead of a pair of 2 ints,

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

I want 3 ints to be connected to each other as one element of a vector.

I realized that one way is to make two sub-nested pairs, but this method becomes messy. I do not know all the details of CPP, so please recommend a simpler method, if available. Thanks.

+4
source share
4 answers

No need to retrain.

 struct Triplet { int one_, two_, three_; }; vector<Triplet> triplets; 
+12
source

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

+13
source

Check the forced tuple http://www.boost.org/doc/libs/1_49_0/libs/tuple/doc/tuple_users_guide.html

You can easily create pairs, triples, fours, up to n-uples!

+3
source

C ++ 11 has std::array , see here . In C ++ 03, I would probably define a 3 int structure and make a vector of them.

-1
source

Source: https://habr.com/ru/post/1411865/


All Articles