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
  • 7
  • Are you sure that this is really the best data structure to represent your data? Because I'm guessing it is not... – Cody Gray Dec 11 '16 at 18:35
  • @Kevin What is I?! How many elements should the vectors have?! What values should be assigned to objects of type int? Can you use the human language or not?! – Vlad from Moscow Dec 11 '16 at 18:36
  • `pair>,vector>> pvp = {vector>(n, {true, 0}), vector>(n, {false, 0})};` But that's a really weird data structure to begin with. – Baum mit Augen Dec 11 '16 at 18:37

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,018
  • 2
  • 28
  • 57
Kevin
  • 2,014
  • 2
  • 20
  • 24
  • 2
    Seems like fair advice, but this would be better posted as a comment, rather than an answer. Especially since it is not "a direct answer to the question". :-) – Cody Gray Dec 11 '16 at 18:45
  • 1
    @CodyGray - sometimes the right answer is to unask the question. – Pete Becker Dec 11 '16 at 19:01
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
  • 230,875
  • 49
  • 477
  • 553
Stack Danny
  • 5,590
  • 1
  • 17
  • 49
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
  • 1,885
  • 14
  • 12