Searching for a container with multiple keys

Hi, I need a container which has multiple keys. The number of keys,
which are related to a value, is not constant, but it is small. (less than 10)
I have tried to do this with std::map, but things are working only with a
constant number of keys per value. Any suggestions?

Something like this

key1 ----|
key2 ----|--------- value1
key3 ----|

key4 ----|--------- value2
key5 ----|
Last edited on
map<string, vector<valueType> >.

That way each key can have a vector of values against it.
A workaround could be to have an std::map of pointers. Each key will map to a single pointer but the pointers can point to the same object.
I have used the previous one, where I have map of pointers, but I have some
problems. This map is, of course, member of a class. But I might have some
memory problems. When I fill the map with keys and values everything seems
to be working, (i print everything and everything is there) but when I do
something else with the map afterwards, for example, at the end of the
program, then pointers are pointing somewhere else. I just want that
pointers are pointing to the same object until they are destroyed.

1
2
3
4
5
6
7
8
9
10
void read(){
   std::vector<ClassA> storage;
   std::map<int, ClassA*> map;

   for (int i=0; i<1000; ++i) {
     .....
    storage.push_back(ClassA());
    map.insert(std::pair<int,ClassA > (storage.back().getKey(), &(storage.back());
  }
}



1
2
3
4
5
6
7
8
9
void run(){  // Member of a class
  void read();
// If I have function that uses a lot of memory here. My pointers
// are pointing somewhere else.
  void high_memory_consumption();
  void do_staff_with_the_map();
// If the same function is here, everything works.
  // void high_memory_consumption();
}

std::vector<ClassA> storage;

The pointers to items in the vector are invalidated when the vector is resized.
For this scenario, use std::list<> instead of a vector.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <vector>
#include <list>

int main()
{
    std::vector<int> vec = { 0, 1, 2, 3, 4 } ;
    std::list<int> lst = { 0, 1, 2, 3, 4 } ;

    std::cout << "before resize:\n" ;
    for( int& i : vec ) if( i == 3 ) std::cout << "  address(vector): " << &i << '\n' ;
    for( int& i : lst ) if( i == 3 ) std::cout << "    address(list): " << &i << '\n' ;

    vec.resize(100) ;
    lst.resize(100) ;

    std::cout << "\nafter resize:\n" ;
    for( int& i : vec ) if( i == 3 ) std::cout << "  address(vector): " << &i << '\n' ;
    for( int& i : lst ) if( i == 3 ) std::cout << "    address(list): " << &i << '\n' ;
}


http://liveworkspace.org/code/4677hD$0
Ok. Thanks but it didn't helped. As I said before I have printed the map at the end of the ( read() ) function and it is ok. But when I am printing the map in the function do_staff_with_the_map() pointers have gone wild.. Storage and map are constant after I have created these. I only have to read constant data from the objects. At least for me this is weird because high_memory_consumption() function has nothing that is related to the map or storage and If I remove that everything works.
Last edited on
> this is weird because high_memory_consumption() function has nothing
> that is related to the map or storage and If I remove that everything works.

What is the code for high_memory_consumption()?
read() function reads a file with some parameters and this map / storage structure should store basically most of the parameters, but not all of them. In the real case, when the code should work, I also need to pass couple of parameters ( just two integers and they are not stored in the map or storage ) from the read function to the high_memory_consumption() function. Now there is only fixed constants. Unfortunately I need to run functions in that order. Next I will try to use shared pointers. Let's see if it works.
> Next I will try to use shared pointers. Let's see if it works.

If it doesn't work with raw pointers, it won't work with shared pointers either.

Repeat: What is the code for high_memory_consumption()?
It initializes some structures like matrices and objects which are members of the class. read, high_memory_consumption, do_some_stuff_with_map are all members of the class too.

You might be right. There might be something wrong in that function. After I commented this function
line-by-line i noticed that the problem disappears after a line. I try to solve this problem. Thanks!
Last edited on
Yes, Thanks for all of you! Especially for @JLBorges. I have no idea what was wrong but made some things differently and everything works now. Thanks again..
Last edited on
Topic archived. No new replies allowed.