0

I need to initialize a vector of pair with (false,false) in each pair of length n.

Something like this, but correct:

vector<pair<bool,bool> > hm(n,(false,false));
2
  • Is it in Java? Please include the language of your choice! Nov 5, 2014 at 10:38
  • Looks like C++ to me.
    – duffymo
    Nov 5, 2014 at 10:38

3 Answers 3

6

std::pair value initializes its members, which means that a default construction will set both members to false. So this will have the desired effect:

vector<pair<bool,bool>> hm(n);
6

Initialize a vector of n pairs in one line like this:

std::vector<std::pair<bool, bool> > hm{n, {false, false}};
3
  • +1: with the stress placed on { instead of ( :) Nov 5, 2014 at 10:45
  • Thanks @quetzalcoatl, typo found!
    – gsamaras
    Nov 5, 2014 at 10:46
  • Too much typing for my liking ;-) Nov 5, 2014 at 10:47
3

C++03 compatible answer:

std::vector<std::pair<bool,bool> > hm(n, std::make_pair(false, false));
1
  • 1
    For C++11 (or newer), see G.Samaras' excellent answer :) Nov 5, 2014 at 10:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.