0

I'd like to initialize:

pair<vector<pair<bool,int>>,vector<pair<bool,int>>> pvp;

so that for all i:

pvp.first[i].first = true;

and

pvp.second[i].first = false;

I know that you could do this with a loop but isn't there any faster way like an initialization for a vector?

Community
  • 1
  • 1
Kevin
  • 43
  • 1
  • 8

3 Answers3

3

Sorry, I do not have a direct answer to the question, but I do not see the the question as the real problem.

Generic data structures are great, but maybe, consider a few classes, instead. That way the individual class constructers can handle the initializations as needed (in smaller pieces).

yuri kilochek
  • 12,709
  • 2
  • 32
  • 59
Kevin
  • 2,234
  • 2
  • 21
  • 26
1

The initialization syntax would be:

pvp{ vector<pair<bool, int>>(5, { true, 0 }), vector<pair<bool, int>>(5, { false, 0 }) };

Now, you didn't specify any length of the array (or what the integer should be), but here's the full approach:

#include <tuple>
#include <vector>
#include <iostream>

using namespace std;

int main(){

    pair<vector<pair<bool, int>>, vector<pair<bool, int>>> pvp{ vector<pair<bool, int>>(5, { true, 0 }), vector<pair<bool, int>>(5, { false, 0 }) };

    for (auto i : pvp.first){
        cout << (i.first ? "true" : "false") << '\n';
    }
    for (auto i : pvp.second){
        cout << (i.first ? "true" : "false") << '\n';
    }

    return 0;
}

Output:

 true
 true
 true
 true
 true
 false
 false
 false
 false
 false

As already mentioned, this implementation is too complex for a simple human reader to understand. Separate it into smaller pieces, though.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Stack Danny
  • 7,754
  • 2
  • 26
  • 55
1

Consider using typedefs to make the code easier to read.

using MyPair = pair<bool,int>;
using MyPairs = vector<MyPair>;
pair<MyPairs,MyPairs> pvp{MyPairs{make_pair(true,10)},
                          MyPairs{make_pair(false,11)}};
Waxrat
  • 2,075
  • 15
  • 13