您的位置:首页 > 其它

map的 增删改查 操作

2013-04-23 16:28 459 查看
整理自网络,也算是原创吧,。。。。
#include <map>
#include <iostream>
#include<fstream>
  using namespace std;
int main()  ////map 的增删改查,
{
     map <string, string > maplist;
     ///step1-------------------增
        ///1.1 用insert函数插入value_type数据,下面举例说明  
      maplist.insert(map<string,string> :: value_type("insert_type:111","123asd"));
      maplist.insert(map<string,string> :: value_type("insert_type:222","123asds"));
      maplist.insert(map<string,string> :: value_type("insert_type:333","123asdss"));
         ///1.2// 用数组方式插入数据,下面举例说明  
    maplist["array_type:111"] = "student_one";  
    maplist["array_type:222"] = "student_two";  
    maplist["array_type:333"] = "student_three";  
    maplist["array_type:444"] = "student_four";   
     ///step2-----------------------删
     map < string, string >::iterator it;
     it = maplist.find("123asd");
			if (it != maplist.end()) {
			     maplist.erase(it );
			} else {
			cout << "not find the element" << endl;
			}
			
		///step3-------------------------------改
			maplist["123asd"]="hello,Neeao";
	////step4-------------------------------查
      it= maplist.find("123asd");
       if(it == maplist.end()) {
       	cout<<"没找到"<<endl;
       }
       else{
       		cout<<"找到, string is:"<<it->second<<endl;
       }
			
     ///------------------将map数据写入文件
      ofstream ofs("/home/sno/file.dat",ios::app);
      map<string, string>::iterator p;
      for(p = maplist.begin(); p != maplist.end(); ++p)
      {
          ofs<<p->first<<" "<<p->second<<endl;
      }
      ofs.close();
      cout<<"write success . write data is :"<<endl;
   for( p = maplist.begin( ); p != maplist.end( ); ++p )
      cout <<  p->first<<"==>"<<p->second<<endl;
      ///-----------------------------------将保存的文件数据读入map
      
       ifstream ifs("/home/sno/file.dat");
        string keys;
        string values;
        while(ifs>>keys>>values)
        {
            maplist.insert(map<string,string> :: value_type(keys,values));  ///;自己的插入,如果有重复的,自动把前面的
        }
        cout <<"---------------read success read data is :"<<endl;
        for( p = maplist.begin( ); p != maplist.end( ); ++p )
      cout <<  p->first<<"==>"<<p->second<<endl; 
   ///输出map大小;
   cout<<"maplist size is:"<<maplist.size() <<endl;
   ////再次测试insert 函数,发现 如果key 已经存在,则insert失败。
   cout<<"----------test  insert  again--------"<<endl;
   maplist.insert(map<string,string> :: value_type("insert_type:333","---------test insert------"));
   	
   	 for( p = maplist.begin( ); p != maplist.end(); ++p )
      cout <<  p->first<<"==>"<<p->second<<endl; 
  
}
/*
map的其他操作:
      begin()          返回指向map头部的迭代器
      clear()         删除所有元素
      count()          返回指定元素出现的次数
      empty()          如果map为空则返回true
      end()            返回指向map末尾的迭代器
      equal_range()    返回特殊条目的迭代器对
      erase()          删除一个元素
      find()           查找一个元素
      get_allocator()  返回map的配置器
      insert()         插入元素
      key_comp()       返回比较元素key的函数
      lower_bound()    返回键值>=给定元素的第一个位置
      max_size()       返回可以容纳的最大元素个数
      rbegin()         返回一个指向map尾部的逆向迭代器
      rend()           返回一个指向map头部的逆向迭代器
      size()           返回map中元素的个数
      swap()            交换两个map
      upper_bound()     返回键值>给定元素的第一个位置
      value_comp()      返回比较元素value的函数
      Map中的元素是自动按key升序排序,所以不能对map用sort函数:(上面的例子打印信息印证了这点)
      Map中的swap不是一个容器中的元素交换,而是两个容器交换;
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: