Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ map<string, vector<char> > access

Tags:

c++

map

stl

vector

I've created a map of vectors that looks like this:

map<string, vector<char> > myMap;
string key = "myKey";
vector<char> myVector;
myMap[key] = myVector;

I want to be able to append 'char's' to the vector in the map but I can't figure out how to access said vector to append once the particular key/value(vector) has been created. Any suggestions? I'm iterating over char's and might be adding a lot to the vector as I go so it would be nice to have a simple way to do it. Thanks.


I would like the vector in map to be appended as I go. I don't need the original vector...I just need to return the map of key/vector's that I've created (after apending) so that I can pass it to another function. What does the * in map* > do? Is that refrencing a pointer? (I haven't gotten there in lecture yet) Also, do I need: myMap[key]->push_back('s'); or myMap[key].push_back('s'); ??

like image 785
MCP Avatar asked Jan 22 '12 10:01

MCP


1 Answers

To append:

myMap[key].push_back('c');

Or use myMap.find, but then you have to check whether you get an end iterator. operator[] returns a reference to the vector.

But this modifies the vector stored in the map, not the original one, since you've stored a copy in the map with myMap[key] = myVector;. If that's not what you want, you should rethink your design and maybe store (smart) pointers to vectors in your map.

like image 185
Fred Foo Avatar answered Oct 19 '22 09:10

Fred Foo