I need to map a single string to multiple strings, to do this i thought about two different solutions:
The first is to map each string to a vector so that when i look at the key i get the vector in return. std::unordered_map<std::string, std::vector<std::string>>
Using this solution means that i need to look for a key only once but then i have to iterate on the array to find the correct string that i need.
The second solution i thought was to use each string contained in the vectors (i know that they are unique) as key and map them to what would've been the key in solution 1. std::unordered_map<std::string, std::string>
Using this solution means that i need to look for a key n times (where n is the length of the array in solution 1) and in my map i have the same value for many keys (i don't know if that matters in the end) but i would directly have the string that i need.
example 1:
std::unordered_map<std::string, std::vector<std::string>> map;
std::vector<std::string> arr = {"hello", "world"};
map["greetings"] = array;
example 2:
std::unordered_map<std::string, std::string> map;
map["hello"] = "greetings";
map["world"] = "greetings";
For the purpose of my program it doesn't matter what string i have in the end (the value from the array of solution 1 or the value from solution 2) as long as i have a way to map them to each other so both solutions are viable. I don't have a way to know in advance the length of the array in solution 1.
Are there any major differences in the two solutions? Which one would be faster/use less memory on paper?
Aucun commentaire:
Enregistrer un commentaire