I'm not sure I understood your code and your question well... but if you want a std::map
associating std::string
keys to int
values, why do you define an array of (key, value) pairs?
What about the following, instead?
std::map<std::string, int> m;
m["hello"] = 42;
m["world"] = 88;
I think if you have an unordered array (like opMap
in your code), if you want to search something you can do a linear search (O(N)
). Only if the array is sorted you can optimize the search using e.g. a binary search with std::lower_bound()
(which has logarithmic asymptotic complexity).
If you want to initialize the map from the content of the opMap
array, you can do something like this:
// opMap is an array of (key, value) pairs
// m is a std::map<std::string, int>
//
// For each item in the array:
for (int i = 0; i < DDG::OP_COUNT; i++)
{
// opMap[i].first is the key;
// opMap[i].second is the value.
// Add current key-value pair in the map.
m[ opMap[i].first ] = opMap[i].second;
}