0

I have the following code:

//MyClass.h
class MyClass {
      typedef std::map<std::string, int> OpMap;
      static const OpMap::value_type opMap[OP_COUNT];

    public:
     //methods
};

//MyClass.cpp
const MyClass ::OpMap::value_type MyClass ::opMap[DDG::OP_COUNT] = {
    MyClass ::OpMap::value_type("hello", 42),
    MyClass ::OpMap::value_type("world", 88),
};

I need to implement function bool findOP(string opKey) which searches for opKey in the opMap.

Looks like I need to use a find method of the map class. But opMap.find(opKey) doesn't work, since opMap is an array of pairs. What is possible to do in order to search effectively for opKey in the opMap?

je4d
  • 7,628
  • 32
  • 46
YAKOVM
  • 9,805
  • 31
  • 116
  • 217

1 Answers1

1

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;
}
Mr.C64
  • 41,637
  • 14
  • 86
  • 162
  • I used value_type for static "map initializatin",but I still want to use the map properties in orer to make effective serch (find) – YAKOVM Nov 19 '12 at 23:04
  • @Yakov: I've added a sample code to initialize the map from the content of the array: just iterate through the array, and add each key-value pair in the map. – Mr.C64 Nov 19 '12 at 23:09
  • I don`t want to initialize the map from the array.I just thought that since the array is "built" from map ,it could be simple convension of array to map – YAKOVM Nov 19 '12 at 23:11
  • @Yakov: If you want to use the "map properties" (as you wrote) you have to fill the map with some data. You have an array of key-value pairs, but it's just an array. You have to put this data in the map if you want to use `std::map` member functions. – Mr.C64 Nov 19 '12 at 23:13
  • @Yakov if you've got C++11 available to you, you can just initialize a map directly. In C++03 you could use boost::assign, see http://stackoverflow.com/q/2172053/1030301 for more info – je4d Nov 19 '12 at 23:15
  • Also, if you have to initialize at runtime, you could write that loop more succinctly as `std::copy(MyClass::opMap, MyClass::opMap + OP_COUNT, std::inserter(realMap));` – je4d Nov 19 '12 at 23:17
  • @je4d : Yes.I know that,but I still use C++10 – YAKOVM Nov 19 '12 at 23:18
  • @Yakov: there ain't such a thing as "C++10"... do you mean Microsoft Visual Studio 2010 whose C++ compiler is a.k.a. "VC10"? – Mr.C64 Nov 19 '12 at 23:19