c++中的map容器

map/multimap基本概念

  1. Map的特性是,所有元素都会根据元素的键值自动排序。Map所有的元素都是pair,同时拥有实值和键值,pair的第一元素被视为键值,第二元素被视为实值,map不允许两个元素有相同的键值
  2. 我们可以通过map的i迭代器改变mao的键值吗?答案是不行,因为map的键值关系到map元素的排列规则,任意改变map键值会严重破坏map的组织,如果想要修改元素的实值,是可以的
  3. Map和list拥有相同的某些性质,当对它的容器元素进行新增操作或者删除操作时,操作之前的所有迭代器,在操作完成之后依然有效,当然被删除的那个元素的迭代器必然是个例外
  4. Multimap和map的操作类似,唯一区别multimap的键值可以重复
  5. map和multimap都是以红黑树为底层实现机制

map 对象的拷贝构造与赋值

  1. map(constmap&mp); //拷贝构造函数
  2. map&operator=(constmap&mp); //重载等号操作符
  3. map.swap(mp); //交换两个集合容器

map 的大小操作

  1. map.size();//返回容器中元素的数目
  2. map.empty();//判断容器是否为空

map 的删除操作

  1. map.clear(); //删除所有元素  map.erase(pos);//删除 pos 迭代器所指的元素,返回下一个元素的迭代器。
  2. map.erase(beg,end); //删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
  3. map.erase(keyElem); //删除容器中 key 为 keyElem 的对组。
  void test02()
	    {
	    	map<int, int>m;
	    	m.insert(pair<int, int>(1, 10));
	    	m.insert(make_pair(2, 20));
	    	m.insert(map<int, int>::value_type(3, 30));
	    	m[4] = 40;
	    
	    	m.erase(1);
	    	for (map<int, int >::iterator it = m.begin(); it != m.end(); it++)
	    	{
	    		cout << "key=" << it->first << "value" << it->second << endl;
	    	}
	    
	    	map<int,int>::iterator pos=m.find(2);
	    	if (pos != m.end())
	    	{
	    		cout << "找到,key值为:" << pos->first << "value" << pos->second << endl;
	    	}
	    	else
	    	{
	    		cout << "未找到" << endl;
	    	}
	    
	    	int num=m.count(3);//map的count要么0要么1
	    	cout << "num=" << num << endl;
	    
	    	//lower_bound(keyElem);//返回第一个key>=keyElem元素的迭代器
	    	map<int, int>::iterator ret = m.lower_bound(3);
	    	if (ret != m.end())
	    	{
	    		cout << "lower_bound中key" << ret->first << "value" << ret->second << endl;
	    	}
	    	else
	    	{
	    		cout << "未找到" << endl;
	    	}
	    
	    	//upper_bound(keyElem);//返回第一个key>keyElem元素的迭代器
	    	 ret = m.upper_bound(3);
	    	if (ret != m.end())
	    	{
	    		cout << "upper_bound中key" << ret->first << "value" << ret->second << endl;
	    	}
	    	else
	    	{
	    		cout << "未找到" << endl;
	    	}
	    
	    	pair<map<int,int>::iterator,map<int,int>::iterator>ret2= m.equal_range(3);
	    	if (ret2.first != m.end())
	    	{
	    		cout << "找到了equal_range中的lower_bound的key" << ret2.first->first << "value" << ret2.first->second << endl;
	    
	    	}
	    	else
	    	{
	    		cout << "未找到" << endl;
	    	}
	    
	    	if (ret2.second != m.end())
	    	{
	    		cout << "找到了equal_range中的upper_bound的key" << ret2.second->first << "value" << ret2.second->second << endl;
	    
	    	}
	    	else
	    	{
	    		cout << "未找到" << endl;
	    	}
	 	  }

map 的查找操作

  1. map.find(key); 查找键 key 是否存在,若存在,返回该键的元素的迭代器;若不存 在,返回 map.end();
  2. map.count(keyElem); //返回容器中 key 为 keyElem 的对组个数。对 map 来说,要 么是 0,要么是 1。对 multimap 来说,值可能大于 1。

map 的插入与迭代器

  1. map.insert(…); //往容器插入元素,返回 pair<iterator,bool>
  2. 在 map 中插入元素的三种方式: 假设 map<int,string>mapStu;
  3. 通过 pair 的方式插入对象 mapStu.insert( pair<int,string>(3,“小张”) );
  4. 通过 pair 的方式插入对象 mapStu.inset(make_pair(-1,“校长-1”));
  5. 通过 value_type 的方式插入对象 mapStu.insert( map<int,string>::value_type(1,“小李”) );
  6. 通过数组的方式插入值 mapStu[3]=“小刘"; mapStu[5]=“小王";
   void test01()
    {
    	map<int, int>m;
    	//插入值
    	//4种方式
    	//第一种
    	m.insert(pair<int, int>(1, 10));
    	//第二种
    	m.insert(make_pair(2, 20));
    	//第三种
    	m.insert(map<int, int>::value_type(3, 30));
    	//第四种 如果保证key存在,可以通过这种方式访问
    	m[4] = 40;
    	for (map<int, int >::iterator it = m.begin(); it != m.end(); it++)
    	{
    		cout << "key=" << it->first << "value" << it->second << endl;
    	}
    
    	if (m.empty())
    	{
    		cout << "空" << endl;
    	}
    	else
    	{
    		cout << "size=" << m.size() << endl;
    	}
    }
注意
  1. 前三种方法,采用的是 insert()方法,该方法返回值为 pair<iterator,bool>
  2. 第四种方法非常直观,但存在一个性能的问题。插入 3 时,先在 mapStu 中查找主 键为 3 的项,若没发现,则将一个键为 3,值为初始化值的对组插入到 mapStu 中, 然后再将值修改成“小刘”。若发现已存在 3 这个键,则修改这个键对应的 value
  3. stringstrName=mapStu[2]; //取操作或插入操作
  4. 只有当 mapStu 存在 2 这个键时才是正确的取操作,否则会自动插入一个实例,键 为 2,值为初始化值。
  //指定排序规则
	    class myCompare
	    {
	    public:
	    	bool operator()(int v1, int v2)
	    	{
	    		return v1 > v2;
	    	}
	    };
	    void test03()
	    {
	    	//从大到小排序
	    	map<int, int,myCompare>m;
	    	m.insert(pair<int, int>(1, 10));
	    	m.insert(make_pair(2, 20));
	    	m.insert(map<int, int>::value_type(3, 30));
	    	m[4] = 40;
	    
	    	for (map<int, int, myCompare>::iterator it = m.begin(); it != m.end(); it++)
	    	{
	    		cout << "key:" << it->first << "value:" << it->second << endl;
	    	}
	    
	    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值