关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻公共列表

STL-map/multimap容器

发布时间:2023-06-30 16:00:47

  • map中所有元素都是pair
  • pair第一个元素为key键值,起到索引作用,第二个元素为value实质
  • 所有元素都会按照key键值自动排序

本质:

  • map/multimap属于关联式容器,底层结构使用二叉树实现

优点:

  • 可以根据key值快速找到value值

map和multimap区别:

  • map不允许容器中有重复的key值元素
  • multimap允许容器中有重复的key值元素

map构造和赋值

构造:

  • mapmp;//默认构造
  • map(const map& mp);//拷贝构造

赋值:

  • map& operator=(const map& mp);//重载=运算符

map容器所有元素都成对出现,插入数据需要使用对组

#include#include#includeusing namespace std; void PrintMap(const map& mp) {  for (map::const_iterator it = mp.begin(); it != mp.end(); it++)  {  //可以用解引用的方法,也可以用间接访问操作符->  cout << (*it).first << " " << it->second << endl;  }  cout << endl; } int main() {  mapmp;  mp.insert(pair(1, 2));  mp.insert(pair(5, 4));  mp.insert(pair(3, 1));  mp.insert(pair(2, 4));  mp.insert(pair(4, 6));  PrintMap(mp);  mapmp1;  mp1 = mp;  PrintMap(mp1);  return 0; }

   


map容器大小和交换

  • .size();//返回容器中元素数目
  • .empty();//判断容器是否为空
  • .swap(mp);//交换两个集合容器
#include#include#includeusing namespace std; void PrintMap(const map& mp) {  for (map::const_iterator it = mp.begin(); it != mp.end(); it++)  {  //可以用解引用的方法,也可以用间接访问操作符->  cout << (*it).first << " " << it->second << endl;  }  cout << mp.size() << mp.empty() << endl; } int main() {  mapmp;  mapmp1;  mp.insert(pair(1, 2));  mp.insert(pair(5, 4));  mp.insert(pair(3, 1));  mp1.insert(pair(2, 4));  mp1.insert(pair(4, 6));  mp1.insert(pair(6, 6));  PrintMap(mp);  PrintMap(mp1);  //交换  mp.swap(mp1);  //验证交换后的结果  PrintMap(mp);  PrintMap(mp1);  return 0; }

   


map插入和删除

  • .insert(elem);//容器中插入元素
    • pair方式
    • make_pair方式
    • value_type方式
    • []下标方式
  • .clear();//清除所有元素
  • .erase(pos);//删除pos迭代器所指的元素,返回下一个元素的迭代器
  • .erase(beg,end);//删除区间[beg,end)内的元素,返回下一个元素的迭代器
  • .erase(key);//删除容器中值为key的元素
#include#include#includeusing namespace std; void PrintMap(const map& mp) {  for (map::const_iterator it = mp.begin(); it != mp.end(); it++)  {  //可以用解引用的方法,也可以用间接访问操作符->  cout << (*it).first << " " << it->second << endl;  }  cout << endl; } int main() {  mapmp;  //第一种插入  mp.insert(pair(2, 1));  //第二种插入,不需要写模板参数  mp.insert(make_pair(1, 3));  //第三种,value_type值类型  mp.insert(map::value_type(3, 6));  //第四种,不建议用于插入  mp[4] = 5;  //下标方式,如果没有找到,会创建一个实值为0的元素  //作用:可以利用key值访问到value  cout << mp[5] << endl;  PrintMap(mp);  //删除  mp.erase(mp.begin());  mp.erase(5);//按照key删除  PrintMap(mp);  return 0; }

   


map查找和统计

  • .find(key);//查找key是否存在,返回该键的元素的迭代器,若不存在,返回.end();
  • .const(key);//统计key的元素个数,对于map,无非是0或1
mapmp; mp.insert(pair(1, 2)); mp.insert(make_pair(2, 2)); mp.insert(map::value_type(3, 1)); if (mp.find(3) != mp.end()) {  cout << "找到了" << mp[3] << endl; }

   


map自定义排序规则

利用仿函数,可以改变排序规则

对于自定义数据类型,map必须指定排序规则,同set容器

#include#include#includeusing namespace std; class myCompare { public:  bool operator()(int v1, int v2)const  {  return v1 > v2;  } }; int main() {  mapmp;  mp.insert(pair(1, 2));  mp.insert(make_pair(2, 2));  mp.insert(map::value_type(3, 1));  for (map::iterator it = mp.begin(); it != mp.end(); it++)  {  cout << it->first << " " << it->second << endl;  }  return 0; }

/template/Home/leiyu/PC/Static