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不是一个容器中的元素交换,而是两个容器交换; */
相关文章推荐
- 利用hashtable模拟实现权限验证(同map思想-->只能有一个用户名,可重复密码)以及增删查改操作
- Web Service中Axis与DotNet互操作最佳实践:传递java.util.Map对象给C#(译)
- 数组转集合增删操作时候的UnsupportedOperationException
- JS新建MAP对象,并对其进行操作
- PHP操作memcache(增删改查)
- android利用SQLiteOpenHelper类实现对数据库的增删查改操作
- 二十三、标准库类型map的使用、map介绍、map常用操作、
- 操作DB~~对数据的增删改查!!sql语句
- 作业员工信息表实现增删改查操作
- Hibernate中Session增删改查操作代码详解
- MyBatis的增删改查的基本操作:
- (AS3)操作数组的每个元素:forEach,every,filter,map,some
- Poj 2503 Babelfish(Map操作)
- mongoVUE的增删改查操作使用说明
- python数据存储系列教程——python中redis数据库操作:连接、增删查改、多级路径
- SQLite常见操作(增删改查)
- Entity Framework 增删改查和事务操作
- Scala集合操作—Map
- 【ssi】增删改查六操作小框架(四)
- C++ map的基本操作和用法