admin 管理员组

文章数量: 1184232


2024年3月8日发(作者:sqlserver可疑数据库恢复)

C++ STL

By Li-Bangzhu

 map解释

map(映射)——经过排序了的二元组的集合,map中的每个元素都是由两个值组成,其中的key(键值,一个map中的键值必须是唯一的)是在排序或搜索时使用,它的值可以在容器中重新获取;而另一个值是该元素关联的数值。比如,除了可以ar[43] ="overripe"这样找到一个数据,map还可以通过ar["banana"] ="overripe"这样的方法找到一个数据。如果你想获得其中的元素信息,通过输入元素的全名就可以轻松实现。

map 一对一的映射的结合,key不能重复。

 map的使用

//代码可直接运行,运行环境 系统:centos 6.5

//此段代码演示了几种不同方法的插入元素,遍历元素,查找,删除元素

#include

#include

#include

using namespace std;

int main()

{

map mymap; //此map的key和value 都是int 型的

map::iterator it; //定义一个迭代子

it = ();

//用数组的方式插入数据

for(int i= 0;i<10;i++)

{

mymap[i]= (i+3);

}

for(it = ();it !=();it++) //遍历所有元素

{

cout<first<<" "<second<

}

cout<

//用insert 的方式插入数据

for(int j=10;j<20;j++)

{

(pair(j,j+10));

}

for(it= ();it != ();)

{

cout<first<<" "<second<

++it;

}

cout<

//insert的另外一中方式插入元素

for(int k =20; k<30 ;k++)

{

(map::value_type(k,k+100));

}

for(it = ();it!=();)

{

cout<first<<" "<second<

++it;

}

cout<<"this size of map"<<()<

for(int index =0;index<();index++)

{

cout<

}

cout<

cout<

cout<

//find

it = (27);

if(it != ())

{

cout<<"find,this value"<

//delete

(it);

}

for(int index =0;index<();index++)

{

cout<

}

cout<

//clear

();

cout<<"the size of mymap"<<()<

}

 更多详情请浏览个人文库:

/p/helpylee

3Q


本文标签: 元素 插入 运行 结合