hmap.h 1.4 KB

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