vector of maps

i am trying to create a vector of maps.

---CODE--------------------------------------------------------------------------
typedef std::map<int,bool> ConnectionTable;
ConnectionTable tempConTable;
std::vector<ConnectionTable> clconTable,svconTable;
svconTable[localindex].insert(std::make_pair(msg->getSrc(), true));
//runtime error in last line
---------------------------------------------------------------------------------

please help

Regards
Aryan
A map is a collection. You are creating a vector of collections, and make_pair() is used to create a single element of the map collection. instead, you should be inserting a map. If you don't need a vector of collections and instead just a collection, just use map<int, bool>.
What happens when you use svconTable.at(localindex).insert(std::make_pair(msg->getSrc(), true)); ?
here svconTable is vector of map
localindex is used for particular map
then i want to insert <key,value> into that particular map

But there is run time error at this line....i don't know what to do

please help

Regards
Aryan
Due to the value-semantics of STL, I recommend that instead of trying to create a vector of maps, create a vector of pointers to maps.

It will make your life easier when initializing (don't forget to delete your allocations before your vector goes out of scope).

Also, make sure that vector of maps or pointer-to-maps is really what you need... ...are there simpler structures which would do what you want to do?
Due to the value-semantics of STL, I recommend that instead of trying to create a vector of maps, create a vector of pointers to maps.


If you are going to do this, use one of boost ptr_containers so you still have exception safety.
You don't really give enough information but I am guessing that you may have not allocated any elements in your vector, and so you are getting some kind of out-of-bounds problem?

If all you want to do is insert a <key, value> into a map stored in a vector...
1
2
3
4
vector<map<int, bool> > Table; //The vector of maps of ints and booleans
map<int, bool> Temp; //A temporary map to be used below
Table.push_back(Temp); //Add a map<int, bool> to the vector
Table.at(0)[1337/*key for the map*/] = true; //Value for the map 
Have you considered what will happen when the vector needs to grow?

You could use a map on its own:
1
2
typedef std::pair<int, int> index_type;  // localindex, source id
typedef std::map<index_type, bool> connection_table_type;
now i create vector of pointer to map rather than vector of maps
but still i am facing a problem
please help

----------------------------CODE---------------------------------------------------------------
1. typedef std::map<int,bool> ConnectionTable;
2. typedef ConnectionTable * ptrtotable;
3. std::vector<ptrtotable> clconTable,svconTable;
4. ptrtotable tblptr;
5. tblptr=svconTable[localindex]; //assume svcontable has pointer to table
6. ((*tblptr).insert(std::make_pair(msg->getAddress(),true)));

RUn tIME Error is coming in line 6(known in gdb back trace)
---------------------------------------------------------------------------------------------------

Aryan
Is tblptr null?

If you had used my data structure above, your code would be:
1
2
connection_table_type clconTable,svconTable;
svconTable[std::mak_pair(localindex, msg->getAddress())] = true;
No pointers, just straightforward value semantics.
Thanks kbw______________ your idea is working
also consider how many maps you expect to have and how small each map is

if you have a gazillion small maps, you may want to use a vector or [] and simply do a linear search instead

read these comments:

http://stackoverflow.com/questions/2975275/how-large-does-a-collection-have-to-be-for-stdmapk-v-to-outpace-a-sorted-std
Topic archived. No new replies allowed.