数据结构应用标准模版库STL—— map的操作
2013-12-06 12:29
316 查看
#include<iostream> #include<map> #include<string> using namespace std; int main() { map<string,int> student; student["a"]=10; student["b"]=20; student["c"]=30; student["d"]=40; student["e"]=50; student["f"]=60; student["g"]=70; student["h"]=80; cout<<"***************************************"<<endl; cout<<"size of student:"<<student.size()<<endl; //打印元素个数 map<string,int>::iterator iter=student.begin (); iter=student.find ("bc"); //查找"bc",如果找到iter->second=true;否则iter->second==false; if(iter->second==false) cout<<"find "<<iter->first<<":"<<iter->second<<endl; else cout<<"no find bc"<<endl; cout<<"a=>"<<student.find("a")->second<<endl; //查找 cout<<"***************************************"<<endl; cout<<"erase(student.begin()): "<<endl; student.erase(student.begin()); //删除第一个元素 cout<<"size of student:"<<student.size()<<endl; iter=student.begin(); while(iter!=student.end()) { cout<<iter->first<<":"<<iter->second<<endl; iter++; } cout<<"***************************************"<<endl; cout<<"erase(student.begin(),student.end()): "<<endl; //删除开始到结束的所有 student.erase(student.begin() ,student.end()); cout<<"size of student:"<<student.size()<<endl; cout<<"***************************************"<<endl;//插入操作 iter=student.begin (); student.insert (pair<string,int>("x",100)); //方法1 student.insert (pair<string,int>("y",110)); student.insert (pair<string,int>("z",120)); student.insert (student.find("z"),pair<string,int>("a",130)); //方法2 cout<<"size of student:"<<student.size()<<endl; iter=student.begin (); while(iter!=student.end()) { cout<<iter->first<<":"<<iter->second<<endl; iter++; } pair<map<string,int>::iterator ,bool > ret; //方法3 ret=student.insert(pair<string,int>("x",111)); if(ret.second==false) //该处应该是ret.second 不能是ret->second { cout<<"element x is already existed"; cout<<" with a value of "<<ret.first->second<<endl; } cout<<"size of student:"<<student.size()<<endl; return 0; }
相关文章推荐
- 数据结构应用标准模版库STL——向量的操作(对排序后的子表进行排序)
- 数据结构应用标准模版库STL——表的操作(没有重复的有序表)
- 数据结构应用标准模版库STL——栈(分解栈元素)
- 数据结构(二)标准模版库STL
- 数据结构应用标准模版库——Sorting
- 数据结构 C++语言描述--应用标准模板库(STL)(第2版)pdf
- STL模版库的vector,map的简单应用
- STL标准模版库---stack操作大全
- 对vector等STL标准容器进行排序操作
- hdu 4022map和list等数据结构的应用
- qt中容器和C++标准模版库(STL)中容器的区别
- STL中map容器的操作
- STL map操作
- STL-----MAP应用
- STL中map、set的数据结构及底层实现
- STL中map、set的数据结构及底层实现
- STL_map操作
- STL中map的基本应用
- C++模版STL中 map 和 string, vector 的用法详解!
- STL基础1:STL标准模版在VS2010中的使用方法