map of lists of vectors

Hi all
I would like to do a map of lists of vectors

I know how long the vectors will be but I don't know how many vectors it will be for each key. Later I would like to get all the vectors that is on a certain key.
How do I push a vector to the bottom of a list on a map. I thought I could write it like this.
1
2
3
4
5
6
tmpvector[0]=a;
tmpvector[1]=b;
tmpvector[2]=c;
tmpvector[3]=d;
tmpvector[4]=e;
maptable[key].push_back(tmpvector); //tmpvector is 5 long 


It doesn't give any error except when I try to reach them later in the code by:

1
2
3
4
5
6
7
8
for ( itmap=maptable.begin() ; itmap != maptable.end(); ++itmap ) {
   cout << "Key = " << (*itmap).first << endl;  //This works fine but under it I would like to get all the vectors
   for ( itlist=itmap->second.begin() ; itlist != itmap->second.end(); ++itlist) {
      cout << (*itlist)[0] << " " << (*itlist)[1] << " " << (*itlist)[2] << " " << (*itlist)[3] << " " << (*itlist)[4] << endl;
   }
   cout << endl;
}

The variables in use is defined like this

1
2
3
4
5
vector<int> tmpvector;
tmpvector.reserve(5);
map<int,list<vector<int> > > maptable;
map<int,list<vector<int> > >::iterator itmap;
list<vector<int> >::iterator itlist;


Can anyone see what's wrong or how to do this or does anyone know of annother way to accomplish what I want to do?
Out of curiosity, of what benefit is making the value a list of vectors? Why not just a single vector? Or a deque? Why split it
into a list of vectors?
Thanks for the quick answer. But it didn't help me much.

Do you mean only a map of vectors? I need access to all vectors that give a certain key. I also need access to them many times. For each key I will have a few different vectors (don't know how many) so that's why I thought it might be best with a map of lists of vectors.

Any idea what I do wrong?

The error msg I recieve is EXC_BAD_ACCESS when I try to reach (*itlist)[0] but I think it's the maptable[key].push_back(tmpvector); that is wrong
a map of a list of vectors?
my spider sense is tingling.

maybe youshould tell us what you are trying to do
rather than how you want it done.

I want to match certain ints that produce a key together with other ints that produce the same key.
so I have a nested for loop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
for (a=1;a<100;a++) {
 for (b=1;b<100;b++) {
  for (c=1;c<100;c++) {
   for (d=1;d<100;d++) {
    for (e=1;e<100;e++) {
     // and these a to e produce a key so I want to store them in a map so I
     // later can reach them quickly and all of the a to e that produce the same key
     // some a to e produce the same key
    }
   }
  }
 }
}

// Here I go into a similar for loop
for (f=1;f<100;f++) {
 for (g=1;g<100;g++) {
  for (h=1;h<100;h++) {
   for (i=1;i<100;i++) {
    for (j=1;j<100;j++) {
     // and these f to j produce a key and if that key exist in my map I would like to match them with all the a to e at that produce the same key
    // a1, b1, c1, d1, e1 with f, g, h, i, j
    // a2, b2, c2, d2, e2, with f, g, h, i, j
    // a3, b3, c3, d3, e3, with f, g, h, i, j
    // a1 to e1 produce the same key as a2 to e2 and so on
    // I don't know how many a to e will produce the same key
    }
   }
  }
 }
}


That's why I thought a map of lists of vectors might be good.

Any ideas???
Last edited on
So, you want more than one values associated to a specific key... Why not use a multimap then? (*)

http://cplusplus.com/reference/stl/multimap/

EDIT: (*) That is, a multimap of vectors of ints
Last edited on
I just started looking at that.

How do I then insert a vector and later access all the vectors at a certain key?

The important part of the program is that the accessing of the vectors is as fast as possible (All the a to e at a certain key).
http://cplusplus.com/reference/stl/multimap/equal_range/
The equal_range() method seems like it will give you two iterators, between which all of the vectors of the given key reside.
Great! Thanks m4ster r0shi and Zhuge!

I'm looking into it now but it seems I still get the EXC_BAD_ACCESS when trying to access the ints in the vector.

I edited the example you sent so it is a multimap of vectors like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
multimap<char,vector<int> > mymm;
multimap<char,vector<int> >::iterator it;
pair<multimap<char,vector<int> >::iterator,multimap<char,vector<int> >::iterator> ret;
vector<int> v;

v.reserve(5);
v[0]=1;
v[1]=2;
v[2]=3;
v[3]=4;
v[4]=5;
mymm.insert(pair<char,vector<int> >('a',v));
v[0]=11;
v[1]=12;
v[2]=13;
v[3]=14;
v[4]=15;
mymm.insert(pair<char,vector<int> >('b',v));
v[0]=21;
v[1]=22;
v[2]=23;
v[3]=24;
v[4]=25;
mymm.insert(pair<char,vector<int> >('b',v));
	
cout << "mymm contains:\n";
for (char ch='a'; ch<='b'; ch++) {
	cout << ch << " =>" << endl;
	ret = mymm.equal_range(ch);
	for (it=ret.first; it!=ret.second; ++it)
		for (int i=0; i<5; i++) {
			cout << " " << (*it).second[i]; //It compiles ok but here I get the EXC_BAD_ACCESS error
		}
		cout << endl;
	}
}


so I guess this mymm.insert(pair<char,vector<int> >('a',v)); or this (*it).second[i] is wrong.

when I do this mymm.insert(pair<char,vector<int> >('a',v)); does then the whole vector come into the multimap?
I solved it!

I changed

1
2
vector<int> v;
v.reserve(5);


to this

 
vector<int> v (5,0);


Even the map of list of vectors work now. So it was nothing really wrong with it.

But one last question. What do you think is faster? A multimap of vectors or a map of lists of vectors?
A multimap of vectors has one less level of indirection. But, perhaps more importantly,
a multimap of vectors is easier to comprehend than a map of lists of vectors, though I
still wonder why you couldn't just have a map of vectors, essentially combining the vectors
whose keys are equal into a single vector.
I always use this chart (http://www.liamdevine.co.uk/code/containers.php ) when i have to chose a new container.
Choosing the appropriate container depending on how you need to store and access data can improve your code performance, and make it easier to program.
Topic archived. No new replies allowed.