mapmultimap容器用法

    科技2022-07-11  102

    基本概念

    map中所有元素都是pair;pair中第一个元素为key值(键值),起到索引作用,第二个元素为value(实值);所有元素都会根据元素的键值自动排序;本质:属于关联式容器,底层是用二叉树实现;优点:可以根据key值快速找到value值;区别: 1)map不允许容器中有重复的key值元素; 2)multimap允许容器中有重复的key值元素;

    常用接口

    构造函数:

    map<T1,T2>m;map(const map &map); 赋值:map& operator(const map &m); #include<iostream> #include<map> using namespace std; void printMap(map<int,int>&m) { for (map<int,int>::iterator it = m.begin(); it != m.end(); it++) { cout << "key=" << it->first << " value=" << it->second << endl; } cout << endl; } void test() { //默认构造 map<int, int>m; m.insert(pair<int, int>(3, 10));//相当于匿名对组 m.insert(pair<int, int>(10, 60)); m.insert(pair<int, int>(1, 30)); printMap(m);//按照key排序 //拷贝构造 map<int, int>m2(m); printMap(m2); //赋值 map<int, int>m3; m3 = m2; printMap(m3); } int main() { test(); system("pause"); return 0; }

    map大小和交换:

    size();empty();swap(st); #include<iostream> #include<algorithm> #include<map> using namespace std; void printMap(map<int,int>&m) { for (map<int,int>::iterator it = m.begin(); it != m.end(); it++) { cout << "key=" << it->first << " value=" << it->second << endl; } cout << endl; } void test() { map<int, int>m; m.insert(pair<int, int>(3, 10)); m.insert(pair<int, int>(10, 60)); m.insert(pair<int, int>(1, 30)); if (empty(m)) { cout << "map为空!" << endl; } else { cout << "map不为空!" << endl; } cout << "map大小为:" << m.size() << endl; } void test2() { map<int, int>m1; m1.insert(pair<int, int>(3, 10)); m1.insert(pair<int, int>(10, 60)); m1.insert(pair<int, int>(1, 30)); map<int, int>m2; m2.insert(pair<int, int>(2, 80)); m2.insert(pair<int, int>(6, 55)); m2.insert(pair<int, int>(7, 20)); m2.insert(pair<int, int>(5, 35)); cout << "交换前:" << endl; cout << "m1:" << endl; printMap(m1); cout << "m2:" << endl; printMap(m2); m1.swap(m2); cout << "交换后:" << endl; cout << "m1:" << endl; printMap(m1); cout << "m2:" << endl; printMap(m2); } int main() { test(); test2(); system("pause"); return 0; }

    插入和删除

    insert(elem);clear();erase(pos);//删除pos迭代器所指的元素erase(beg,end);erase(key);//删除容器中键值为key的元素 #include<iostream> #include<algorithm> #include<map> using namespace std; void printMap(map<int,int>&m) { for (map<int,int>::iterator it = m.begin(); it != m.end(); it++) { cout << "key=" << it->first << " value=" << it->second << endl; } cout << endl; } void test() { map<int, int>m; //四种插入方式 m.insert(pair<int, int>(3, 10)); m.insert(make_pair(10, 60)); m.insert(map<int, int>::value_type(1, 30)); m[4] = 40; //如果key已存在,会覆盖对应的value值 printMap(m); m.erase(10); printMap(m); m.erase(m.begin()); printMap(m); } int main() { test(); system("pause"); return 0; }

    查找和统计

    find(key);count(key); 排序 #include<iostream> #include<algorithm> #include<map> using namespace std; //仿函数 降序排序 class myCompare { public: bool operator()(int v1, int v2) { return v1 > v2; } }; void test() { map<int, int, myCompare>m; m.insert(pair<int, int>(3, 190)); m.insert(pair<int, int>(6, 160)); m.insert(pair<int, int>(2, 150)); for (map<int, int, myCompare>::iterator it = m.begin(); it != m.end(); it++) { cout << "key=" << it->first << " value=" << it->second << endl; } cout << endl; } int main() { test(); system("pause"); return 0; }

    Processed: 0.014, SQL: 8