简体   繁体   中英

Easy way to make vector triplet in CPP

What would be an easy way to construct a vector triplet of ints in CPP?

ie instead of a pair of 2 ints ,

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

I want 3 int's tied together as one element of the vector.

I realized one way would be to make 2 sub-nested pairs, but this method gets messy. I am not aware of all the details of CPP, therefore please recommend an easier way if available. 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

You can easily create Pairs, triples, quadruples, up to n-uples!

In C++11, there is std::array , see here . In C++03, I would probably define a struct of 3 int s and make a vector of those.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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