C++ map用法

来源:互联网 发布:知天下图片野战 编辑:程序博客网 时间:2024/05/21 10:00

 由于map和其他STL中有相同性,因此可以用在其他的容器中

由于习惯原因下文中将通常说的Keys说成“搜索项”

寻找在map中的当前值的函数


.find()函数

// map::find#include <iostream>#include <map>using namespace std;int main (){  std::map<char,int> mymap;  std::map<char,int>::iterator it;  mymap['a']=50;  mymap['b']=100;  mymap['c']=150;  mymap['d']=200;cout<<mymap.find('b')->second<<endl ;cout<<mymap.find('f')->first<<' '<<mymap.find('f')->second<<endl;//如果没有则返回的是一个未知的字符和地址,也就是不确定的if(mymap.find('f')->first=='f')cout<<' '<<mymap.find('f')->second<<endl;return 0;}

 


寻找map变量的起始地址

.begin()

(map中最基本的函数)

注意begin()函数是始终指向开始的即使是删掉第一个值(详见erase())

// map::begin/end#include <iostream>#include <map>int main (){  std::map<char,int> mymap;  std::map<char,int>::iterator it;  mymap['b'] = 100;  mymap['a'] = 200;  mymap['c'] = 300;  // show content:  std::map<char,int>::iterator it;  for (it=mymap.begin(); it!=mymap.end(); ++it)    std::cout << it->first << " => " << it->second << '\n';  return 0;}


 

清空map所有变量函数

clear()

清空所有的序列,长度变为0

<em>#include <iostream>#include <map>using namespace std;int main (){  std::map<char,int> mymap;  mymap['x']=100;  mymap['y']=200;  mymap['z']=300;mymap.clear();cout<<mymap.size()<<endl;return 0;}</em>


运行结果:

0

 

查找在map变量中是否存在当前值

count()

由于在map中所有的索引项都不存在重复的现象,所以此函数只能返回1(能查找到)和0(未查找到)
此函数和find()的区别;因为find返回的可以是找到的索引项,也可以是索引项所含的数据,但是
在count中不可以这样用

//map::count#include <iostream>#include <map>using namespace std;int main (){  std::map<char,int> mymap;  char c;  mymap ['a']=101;  mymap ['c']=202;  mymap ['f']=303;  for (c='a'; c<'h'; c++)  {      cout << c;    if (mymap.count(c)>0)      cout << " is an element of mymap.\n";    else      cout << " is not an element of mymap.\n";  }  return 0;}

运行结果:

a is an element of mymap.
b is not an element of mymap.
c is an element of mymap.
d is not an element of mymap.
e is not an element of mymap.
f is an element of mymap.
g is not an element of mymap.


 

 

查找reverse倒置排序后的首地址函数

rbegin()

返回的是在revese中的首地址

// map::rbegin/rend#include <iostream>#include <map>int main (){  std::map<char,int> mymap;  mymap['x'] = 100;  mymap['y'] = 200;  mymap['z'] = 300;  // show content:  std::map<char,int>::reverse_iterator rit;  for (rit=mymap.rbegin(); rit!=mymap.rend(); ++rit)    std::cout << rit->first << " => " << rit->second << '\n';  return 0;}


运行结果:

z => 300
y => 200
x => 100

 

判断map变量是否为空函数

.empty() 返回的是该map是否为空,空返回1;否则返回0

// map::empty#include <iostream>#include <map>using namespace std;int main (){  std::map<char,int> mymap;  mymap['a']=10;  mymap['b']=20;  mymap['c']=30;  while (!mymap.empty())  {    std::cout << mymap.begin()->first << " => " << mymap.begin()->second << '\n';    mymap.erase(mymap.begin());  }  cout<<mymap.size();//长度变为零  return 0;}

运行结果:

a => 10
b => 20
c => 30
0

查找结束位置函数

end()

// map::begin/end#include <iostream>#include <map>int main (){  std::map<char,int> mymap;  mymap['b'] = 100;  mymap['a'] = 200;  mymap['c'] = 300;  // show content:  for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)    std::cout << it->first << " => " << it->second << '\n';  return 0;}

运行结果:

a => 200
b => 100
c => 300

erase()

擦除的可以是一个元素,也可以是一个范围

// erasing from map#include <iostream>#include <map>using namespace std;int main (){  std::map<char,int> mymap;  std::map<char,int>::iterator it;  // insert some values:  mymap['a']=10;  mymap['b']=20;  mymap['c']=30;  mymap['d']=40;  mymap['e']=50;  mymap['f']=60;  it=mymap.find('b');  mymap.erase (it);                   // erasing by iterator  mymap.erase ('c');                  // erasing by key  it=mymap.find ('e');  mymap.erase ( it, mymap.end() );    // erasing by range  // show content:  for (it=mymap.begin(); it!=mymap.end(); ++it)    std::cout << it->first << " => " << it->second << '\n';  cout<<mymap.size();//仅剩下a和d所在的两个数了  return 0;}

运行结果:
a => 10
d => 40
2

插入函数

insert()

函数

#include <iostream>#include <map>using namespace std;int main (){   map<char,int> mymap;  // 第一种插入方式(只插入单个值)                                                                                                                 mymap.insert ( pair<char,int>('a',100) );  mymap.insert ( pair<char,int>('z',200) );    //下面是用来显示insert函数的返回值和插入成功与否的判断  //如果只是研究插入方法的话,此段可以不研究   pair< map<char,int>::iterator,bool > ret;//此处是设置insert的返回类型  ret = mymap.insert (  pair<char,int>('z',500) );//可以将该处的'z'修改成'x',观察当插入成功时候的返回值 if (ret.second==false)//如果是已经存在的就返回0 {     cout << "element '"<<ret.first->first<<"' already existed";   cout << " with a value of " << ret.first->second << '\n';  }   else  {      cout << "element '"<<ret.first->first<<"' creat successfully";   cout << " with a value of " << ret.first->second << '\n';   }  // 第二种插入方式 (插入到指定位置):   map<char,int>::iterator it = mymap.begin();  mymap.insert (it,  pair<char,int>('b',300));  // max efficiency inserting  mymap.insert (it,  pair<char,int>('c',400));  // no max efficiency inserting  // 第三种插入方式(插入一定范围的数值)                                                                                                         map<char,int> anothermap;  anothermap.insert(mymap.begin(),mymap.find('c'));//mymap.find('c')插入的末地址,但是插入时不包括改地址 //第四种插入方式(插入单个值)  mymap.insert(map<char,int>::value_type('x',20000000));//第五种 (不是经常用的插入方式)(只插入单个值)      mymap['f']=100000000;   // showing contents:   cout << "mymap contains:\n";  for (it=mymap.begin(); it!=mymap.end(); ++it)     cout << it->first << " => " << it->second << '\n';   cout << "anothermap contains:\n";  for (it=anothermap.begin(); it!=anothermap.end(); ++it)     cout << it->first << " => " << it->second << '\n';  return 0;}



 

运行结果:

element 'z' already existed with a value of 200
mymap contains:
a => 100
b => 300
c => 400
f => 100000000
x => 20000000
z => 200
anothermap contains:
a => 100
b => 300

 

 

测试map长度的函数

size()

运行结果:

// map::size#include <iostream>#include <map>int main (){  std::map<char,int> mymap;  mymap['a']=101;  mymap['b']=202;  mymap['c']=302;  std::cout << "mymap.size() is " << mymap.size() << '\n';  return 0;}


 

3

 

 

max_size()

// map::max_size#include <iostream>#include <map>int main (){  int i;  std::map<int,int> mymap;  if (mymap.max_size()>1000)  {    for (i=0; i<1000; i++) mymap[i]=0;    std::cout << "The map contains 1000 elements.\n";  }  else std::cout << "The map could not hold 1000 elements.\n";  return 0;}


 

 

附加map的特点:

入过出现在容器map中不存在的索引项,则自动生成一个新的空间

// accessing mapped values#include <iostream>#include <map>#include <string>using namespace std;int main (){  map<char,string> mymap;  mymap['a']="an element";  mymap['b']="another element";  mymap['c']=mymap['b'];  cout << "mymap now contains " << mymap.size() << " elements.\n";  cout << "mymap['a'] is " << mymap['a'] << '\n';cout << "mymap['b'] is " << mymap['b'] << '\n';  cout << "mymap['c'] is " << mymap['c'] << '\n';  cout << "mymap['d'] is " << mymap['d'] << '\n';//注意本行输出结果与find找不到时的返回值的不同这个返回的是空字符串  cout << "mymap now contains " << mymap.size() << " elements.\n";  if(mymap['d']=="\0")         //返回的是空字符串    cout<<"This is an empty string";  return 0;}

未完待续

 参考网站:http://www.cplusplus.com/reference/map/map/max_size/

 

 

 

 

 

 

 

0 0
原创粉丝点击