1 #ifndef STRIDMAP_H 2 #define STRIDMAP_H 3 4 #include <vector> 5 #include <string> 6 #include <map> 7 8 class StrIdMap { 9 std::map<std::string, unsigned long> _toId; 10 std::vector<std::string> _toStr; 11 public: getStr(unsigned long id)12 std::string getStr(unsigned long id) { 13 return _toStr[id]; 14 } 15 16 // If the map does not contain the element, the element is added to the end 17 // of the _toStr vector and the vector index is stored in the _toId map. 18 // Or if the map does contain the element, "second" (i.e., the id) is returned. getId(std::string str)19 unsigned long getId(std::string str) { 20 std::map<std::string, unsigned long>::iterator f = _toId.find(str); 21 unsigned long id; 22 if (f == _toId.end()) { 23 id = _toId.size(); 24 _toId[str] = id; 25 _toStr.push_back(str); 26 return id; 27 } 28 else { 29 return f->second; 30 } 31 } 32 }; 33 34 #endif 35