map容器的基本用法(代码版)


#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{map<int,int>m;
    map< int, int >::iterator  kan1;
map< int, int >::iterator  kan;
int a;
m[1]=3;
m[3]=45415145;
m[6]=2;
m[8]=415;
//while(cin>>a,a!=0)
  // {

//m[a]++;

  // }//用数组方式插入数据...当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,


       kan = m.find(1);//要删除1,用迭代器删除


kan1=m.find(6);

//m.erase(kan,kan1);//批量删除。前闭后开。
m.erase(kan1);//单个删除

 

//m.erase(1);//要删除1,用关键字删除
//int p3=m.erase(1);//如果删除了会返回1,否则返回0
//cout<<p3<<endl;

   //m.insert(map<int, int> :: value_type(1, 1));//用insert函数插入value_type数据,
//m[1]=4;

    for(kan=m.begin();kan!=m.end();kan++)
        cout<<kan->first<<"   "<<kan->second<<endl;

//kan=m.find(1);//用find函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,它返回数据所在位置的迭代器,如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器
//if(kan!=m.end())
    //cout<<kan->second<<endl;
//int p1=m.empty();
//cout<<p1<<endl;
//m.clear();//清空map中的数据可以用clear()函数
//p1=m.empty();//判定map中是否有数据可以用empty()函数,它返回true则说明是空map
//cout<<p1<<endl;

//int p=m.count(3);//用count函数来判定关键字是否出现,其缺点是无法定位数据出现位置
//cout<<p<<endl;

//cout<<m.size()<<endl;//.map的大小

 

    return 0;

}

 

posted @ 2014-07-22 16:36  冷夏的博客园  阅读(659)  评论(0编辑  收藏  举报