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

I created a vector map that looks like this:

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

I want to be able to add 'char' s' to a vector on the map, but I cannot figure out how to access the specified vector to add when a specific key / value (vector) was created. Any suggestions? I repeat char and can add a lot to the vector as I go, so it would be nice to have an easy way to do this. Thank.


I would like the vector on the map to be added when I go. I don’t need the original vector ... I just need to return the key / vector map that I created (after it appeared) in order to pass it to another function. What does * in the map *> do? Is this a pointer override? (I have not lectured yet) Also, I need: MyMap [key] β†’ push_back ('s'); or MyMap [key] .push_back ('S'); ??

+5
source share
4 answers

To add:

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

Or use myMap.find, but then you need to check if you have an iterator end. operator[]returns a link to vector.

vector, map, , map myMap[key] = myVector;. , , , , () .

+7

, :

string key = "something";
char ch = 'a'; // the character you want to append

map<string, vector<char> >::iterator itr = myMap.find(key);
if(itr != myMap.end())
{
    vector<char> &v = itr->second;
    v.push_back(ch);
}

map::operator[] , , :

vector<char> &v = myMap[key]; // a map entry will be created if key does not exist
v.push_back(ch);

:

myMap[key].push_back(ch);
+3

, , , , . , 'a':

myMap[key].push_back('a');
+1

. vector<char>* vector<char>, map. . :

map<string, vector<char>* > myMap;
string key = "myKey";
vector<char>* myVector = new vector<char>();
myMap[key] = myVector;
myMap[key]->push_back('S');
0

All Articles