STL two dimensional array of sets question

P

Phil

Hello,

Yet another question I'm afraid. I'm trying to construct a two dimensional
array of sets (9 X 9).

The first line of code gives me a single array of nine sets and the second
line is my feeble attempt to expand the first line to a two dimensional
array of sets.

vector< set<int> > setOne(9); // 9 sets

vector< vector<int> > setTwo<int> > test(81); // 9 X 9 sets

I've tried all sorts of combinations without success. Can someone tell me
where I've gone wrong?

In case I haven't explained this very well, this is what I'm trying to
achieve for example:

setTwo[0][0].insert(2)
 
R

Rolf Magnus

Phil said:
Hello,

Yet another question I'm afraid. I'm trying to construct a two dimensional
array of sets (9 X 9).

The first line of code gives me a single array of nine sets and the second
line is my feeble attempt to expand the first line to a two dimensional
array of sets.

vector< set<int> > setOne(9); // 9 sets

vector< vector<int> > setTwo<int> > test(81); // 9 X 9 sets

I've tried all sorts of combinations without success. Can someone tell me
where I've gone wrong?

Well, if you want a set of int named x, you write:

set<int> x;

If you want a vector of set of int, you write:

vector<set<int> > x;

So if you want a vector of vector of set of int, it must be:

vector<vector<set<int> > > x;

To initialize it with 9x9 sets, you'd need something like:

vector<vector<set<int> > > setTwo(9, vector<set<int> >(9));

This will create a vector of 9 vectors, each of which contains 9 sets of
int.
 
J

joosteto

Phil said:
Hello,

Yet another question I'm afraid. I'm trying to construct a two dimensional
array of sets (9 X 9).

The first line of code gives me a single array of nine sets and the second
line is my feeble attempt to expand the first line to a two dimensional
array of sets.

vector< set<int> > setOne(9); // 9 sets

vector< vector<int> > setTwo<int> > test(81); // 9 X 9 sets

#include <set>
#include <vector>
using namespace std;

vector< vector< set<int> > > vvs;

int main(){
set<int> s1, s2
set<int>::iterator si;
vector<set<int> > vs;

s1.insert(12354);
s2.insert(3244);
vs.push_back(s1);
vs.push_back(s2);
vvs.push_back(vs);
vvs[0][1].insert(5466);
si=vvs[0][1].find(3244);
}

The above isn't a 9x9 array yet, but a 1x2. To make it 9x9, you'll have
to create 9 vectors of each 9 sets, and add them to vvs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top