map基本概念
map中的所有元素都是pair对组,高效率,pair中的第一个元素为key(键值),起到索引作用,第二个元素为value(实值),所有元素都会根据元素的键值自动排序。map/multimap属于关联式容器,底层结构是用而二叉树实现。可以根据key值快速找到value值。map和multimap的区别是map不允许容器中有重复key值元素;multimap允许容器中有重复key值元素。
map构造和赋值
1、map<T1,T2> mp;
默认构造函数
2、map(const map &mp);
拷贝构造函数
3、map& operator=(const map &mp);
赋值
void p(const map<int,int>&s) {
for (map<int,int>::const_iterator it = s.begin();it != s.end();it++) {
cout << "Key:" << (*it).first << "\tValue:" << it->second << endl;;
}
cout << endl;
}
void test1() {
map<int, int> m;
m.insert(pair<int, int>