I have a map that maps strings to vectors of strings:
std::unordered_map<std::string, std::vector<std::string>>> myMap;.
Is there a nice way (as little code as possible while still being readable) to append a value to the vector of a given key?
How to handle the case of adding a value to a vector for a new key for which the vector hasn't been initialized yet?
You want:
myMap["key"].push_back("string");
If the vector for this key doesn't exist, it will be created automatically.
Related
I am trying to make a vector that contains vectors, that contains a string. I am unsure on how I would go about retrieving said string. I've tried vector.at() but I'm not sure how to make a sort of 'multi-layered' version of it.
This is the value inside of my code:
std::vector<std::vector<std::string>> dialoguestore;
There are many ways to go about this with a 2D vector.
Option 1: at():
dialoguestore.at(index).at(index2);
Option 2: operator[]:
dialoguestore[index][index2]
If you have a multidimensional vector, use the ways you normally access a vector, but for the number of dimensions.
I'm a smidge rusty on using map and need a lil' help; I've declared the below.
std::map<std::string, std::vector<double>> myMap;
I'm periodically reading JSON data, where the ordering of data may sometimes change, or new data elements appear. Other parts of my code will loop through the JSON and extract two variables (jsonLabel, a string), and the associated value as a double (latestDouble).
If jsonLabel already exists, I want to add the associated latestDouble to the end of the vector; if it doesn't exist, make the new Key and start a vector.
I've tried the below; however I keep crashing. I'm presuming it's because map actually isn't smart enough to insert latestDouble at the end of the vector in the map.
myMap.insert(std::make_pair(jsonLabel, latestDouble));
Pseudo Example:
JSON parse #1: [A,43],[B,10],[C,9]
JSON parse #2: [A,10],[C,4],[B,3] /// Change in ordering
JSON parse #2: [A,8],[B,7],[C,2],[D,1] /// New element
Should result in:
A: 43,10,8
B: 10,3,7
C: 9,4,2
D: 1
Thanks for any help!
If the string in jsonLabel already exists as a key in the map, then insert will not insert anything into the map.
And the map insert function is to insert into the map itself. The pair should be a pair of the key and value types (where the value should be a vector of doubles).
What you seem to want is simply
myMap[jsonLabel].push_back(latestDouble);
The operator[] function will create a default-constructed value if the key doesn't exist, and as such will create an empty vector for you.
I want to create a map of vectors. I want the vector to be a private member variable however so when I need to increase the size of the vector for a particular key in the map it does it for all other keys in the map also(would that work?). This will be a map of vectors(of ints) where the keys are strings. My question is how to access a particular element in the vector to change is value in C++. Something along the lines of map_name['word'].[3] = 2 if i wanted to set the third value of the vector of "word" to 2.
enter image description here
enter image description here
Im still having trouble figuring out how to make it so the size of each vector for all the keys in the maps is modifiable so i can increase the size of each vector at any point along the program. This is b/c the vector size is unknown at runtime and iterating through each element in the map to change the vector size will take too long.
The pattern is recursive.
That is, when you do:
expression[key] = value;
your expression doesn't have to just be a variable name; it can be a more complex expression, such as map_name["word"].
So:
map_name["word"][3] = 2;
Regarding the first question, yes it is possible as mentioned in one of the comments, you can make your imaginary class to do that.
And in the second question, you'll have to access an element of a vector which is an element of a map like this:
map1["abc"][1] = 2
The '.' you added was unnecessary because you're accessing an element inside another element, just like a 2D array
The following works:
std::map<std::string, Animal*> animalMap;
animalMap["KillerRabbit"] = new KillerRabit;
But what if I wanted to do this?
animalMap["KillerRabbit"]["White"] = new KillerRabit;
I have no idea what the 'official' name for the indices brackets are, knowing them would help immensely while Googling =p
What you are looking for is a map of maps:
std::map<std::string, std::map<std::string, Animal*>> animalMap;
Now each value stored in animalMap is itself a std::map. The key type for both the outer and inner maps are std::string.
The [...] syntax is the subscript operator. More specifically, though, you subscript a map with keys. Keys are mapped to values.
sftrabbit gives the canonical way to do it. If you don't want multiple map look ups per key you could also use std::pair as a map key.
Here is an example of doing it that way.
I have a problem with the creation of a hash of arrays. I need a Single Key - Multi Data system:
multimap <Type, vector<type> > var;
But how I can add elements to the vector?
Example: key = 3;
Now I need to append some elements into the vector whose key is 3.
Creating a temp-vector not an answer because I don't know when I need to input element into the vector with the current key.
sorry, understand my problem. i need fast-access struct, that will be operate with ~50,000 words with length ~20 each.
and i need something like tree.
also, have question:
how quick STL-structures, like vector,map,multimap and other?
What's wrong with std::map <KeyType, std::vector<SomeType> >, or some other collection as the value type? This gives you control over how to operate on the value collection. A multimap to me seems like a low-level form of std::map <KeyType, std::list<SomeType> >, but with none of the flexibility of a list.
To find the answer to your question you can look at the slides under point 6. at this site https://ece.uwaterloo.ca/~ece250/Lectures/Slides/
Hope that helps!