c++ 28 map


#include <iostream>
using namespace std;
//map 关联式容器,基于key  value 存储的  哈希表的变种  内部红黑树
// map<string,int> map; 模版类
int main(void)
{
   return 0;
}


4中插入方法 key 相同情况下,和简单遍历

#include <iostream>
#include <map>
#include <string>
using namespace std;
//map 关联式容器,基于key  value 存储的  哈希表的变种  内部红黑树
// map<string,int> map; 模版类
int main(void)
{
	//key 有一定的限制,以后在研究
	//插入到map 容器内部的元素是按照key 从小到大来排序 
	//所以key 类型一定要重载< 运算符 进行比较排序,自定义的类型一定要重载< 运算符
	map<string,int> mapTest;//模版类 构造类对象
	//插入数据 有四种方法
	//1, 下标
	mapTest["eee"]=500;
	mapTest["aaa"]=100;//下标也能接收字符串参数  map 内部 重载了 int& operator[](const string& index);  2个步骤;①插入一个key==aaa 的元素,初始值是默认int 的0。②,把100赋值给int.效率比较低,但是比较直观
	mapTest["eee"]=300;//key 的值如果有重复的,就改变成最后一次插入的。 但是仅仅局限与[] 这种方式,其他三种方式,根本不允许插入。
	//2,insert  valuse_type
	mapTest.insert(map<string,int>::value_type("bbb",200));// map<string,int>::value_type 这是一个类类型。 构造了一个value_type 的对象 把对象插入到map 容器当中
	mapTest.insert(map<string,int>::value_type("bbb",2000));
	//3,pair 对象 
	mapTest.insert(pair<string,int>("ccc",300));//pair 是一个类类型 std 省略
		mapTest.insert(pair<string,int>("ccc",3000));
	//make pair 函数  返回的就是pair 跟3是等价的
	mapTest.insert(make_pair("ddd",400));//std 可以省略
	mapTest.insert(make_pair("ddd",4000));
	//遍历 迭代器
	map<string,int>::const_iterator it;
	for(it=mapTest.begin();it!=mapTest.end();++it)
	{
		cout<<it->first<<"  "<<it->second<<endl;   //key 
	}

	//先插入的500 发现会自动排序 按照key 从小到大进行排序的。
   return 0;
}

查找的两种方式

#include <iostream>
#include <map>
#include <string>
using namespace std;
int main(void)
{
	map<string,int> mapTest;//模版类 构造类对象
	mapTest["eee"]=500;
	mapTest.insert(map<string,int>::value_type("bbb",200));// map<string,int>::value_type 这是一个类类型。 构造了一个value_type 的对象 把对象插入到map 容器当中
	mapTest.insert(pair<string,int>("ccc",300));//pair 是一个类类型 std 省略
	mapTest.insert(make_pair("ddd",400));//std 可以省略

	//查找key=aaa 的值
	//第一种查找
	int n=mapTest["eee"];//不存在的返回值是0 有缺陷
	cout<<n<<endl;
	mapTest["eee"]=3;//也可以修改
	//第二种
	map<string,int>::iterator itfind;//这个迭代器不是const_iterator

	itfind=mapTest.find("ccc");
	if(itfind!=mapTest.end())
	{
		itfind->second=3000;
	}else
	{
	    cout<<"not found"<<endl;
	}

	////遍历 迭代器
	map<string,int>::const_iterator it;
	for(it=mapTest.begin();it!=mapTest.end();++it)
	{
		cout<<it->first<<"  "<<it->second<<endl;   //key 
	}
   return 0;
}

三种删除方式

#include <iostream>
#include <map>
#include <string>
using namespace std;
int main(void)
{
	map<string,int> mapTest;//模版类 构造类对象
	mapTest["eee"]=500;
	mapTest.insert(map<string,int>::value_type("bbb",200));// map<string,int>::value_type 这是一个类类型。 构造了一个value_type 的对象 把对象插入到map 容器当中
	mapTest.insert(pair<string,int>("ccc",300));//pair 是一个类类型 std 省略
	mapTest.insert(make_pair("ddd",400));//std 可以省略

	//把关键字是bbb 的删除
	//①种方式
	mapTest.erase("bbb");
	//②种方式
	map<string,int>::const_iterator iterase;
	iterase=mapTest.find("ccc");
	if(iterase!=mapTest.end())
	{
	//找到 删除
		mapTest.erase(iterase);
	}

	//③遍历的时候删除 跟vector 一样 这里没有体现 见:vector
	////遍历 迭代器
	map<string,int>::const_iterator it;
	for(it=mapTest.begin();it!=mapTest.end();++it)
	{
		cout<<it->first<<"  "<<it->second<<endl;   //key 
	}
   return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值