自用双索引map

来源:互联网 发布:淘宝客服最重要的三点 编辑:程序博客网 时间:2024/06/06 03:58

注:并非支持所有数据结构


#include <map>template<class Key1, class Key2, class Value>class DMap{public:bool contains(const std::pair<Key1, Key2>& key){return m1.count(key.first) == 1;}bool containsByFirst(const Key1 first){return m1.count(first) == 1;}bool containsBySecond(const Key2 second){return m2.count(second) == 1;}bool erase(std::pair<Key1, Key2> key){m1.erase(key.first);m2.erase(key.second);m.erase(key);return true;}bool eraseByFirst(Key1 first){m.erase(make_pair(first, m1[first]));m2.erase(m1[first]);m1.erase(first);return true;}bool eraseBySecond(Key2 second){m.erase(make_pair(m2[second], second));m1.erase(m2[second]);m2.erase(second);return true;}int size(){return m1.size();}/*void list(){for (std::map<std::pair<Key1, Key2>, Value>::iterator it = m.begin(); it != m.end(); ++it)std::cout << "m[" << it->first.first<< "," << it->first.second << "]=\"" << it->second<< "\"" << std::endl;std::cout<<std::endl;}*/Value& operator [] (const std::pair<Key1, Key2>& key){if (m1.count(key.first) == 0)m1[key.first] = key.second;if (m2.count(key.second) == 0)m2[key.second] = key.first;return m[key];}private:std::map<Key1, Key2> m1;std::map<Key2, Key1> m2;std::map<std::pair<Key1, Key2>, Value> m;};


0 0
原创粉丝点击