hmap.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #ifndef HV_MAP_H_
  2. #define HV_MAP_H_
  3. #include <map>
  4. #include <string>
  5. // MultiMap
  6. namespace std {
  7. /*
  8. int main() {
  9. std::MultiMap<std::string, std::string> kvs;
  10. kvs["name"] = "hw";
  11. kvs["filename"] = "1.jpg";
  12. kvs["filename"] = "2.jpg";
  13. //kvs.insert(std::pair<std::string,std::string>("name", "hw"));
  14. //kvs.insert(std::pair<std::string,std::string>("filename", "1.jpg"));
  15. //kvs.insert(std::pair<std::string,std::string>("filename", "2.jpg"));
  16. for (auto& pair : kvs) {
  17. printf("%s:%s\n", pair.first.c_str(), pair.second.c_str());
  18. }
  19. auto iter = kvs.find("filename");
  20. if (iter != kvs.end()) {
  21. for (int i = 0; i < kvs.count("filename"); ++i, ++iter) {
  22. printf("%s:%s\n", iter->first.c_str(), iter->second.c_str());
  23. }
  24. }
  25. return 0;
  26. }
  27. */
  28. template<typename Key,typename Value>
  29. class MultiMap : public multimap<Key, Value> {
  30. public:
  31. Value& operator[](Key key) {
  32. auto iter = this->insert(std::pair<Key,Value>(key,Value()));
  33. return (*iter).second;
  34. }
  35. };
  36. }
  37. #ifdef USE_MULTIMAP
  38. #define HV_MAP std::MultiMap
  39. #else
  40. #define HV_MAP std::map
  41. #endif
  42. // KeyValue
  43. typedef std::map<std::string, std::string> keyval_t;
  44. typedef std::MultiMap<std::string, std::string> multi_keyval_t;
  45. namespace hv {
  46. typedef HV_MAP<std::string, std::string> KeyValue;
  47. }
  48. #endif // HV_MAP_H_