linux下练习 c++ 关联式容器共性测试,使用
2012-10-25 12:46
489 查看
/* 关联式容器共性:二叉查找树实现,自动根据关键字排序,自动平衡 set<K>,multiset<K>,map<K,V>,multimap<K,V> 查找:.find(key) 失败返回.end() 统计:.count(key) 删除:.erase(key) 插入:.insert(element) 区间:.lower_bund(key) //取得关键字为key的第一个元素位置 .upper_bound(key) //取得关键字为key的最后一个元素之后的位置 .equal_range(key) 取得关键字为key的区间,返回pair 构造函数可用比较函数作为参数 bool func(K a,K b) */ #include<iostream> #include<set> #include<string> using namespace std; #include "print.h" struct person { string name; int age; public: person(const char* n,int a):name(n),age(a){} }; bool operator<(const person& a,const person& b) { return a.age<b.age||(a.age==b.age&& a.name<b.name);//找的时候按这个找 } ostream& operator<<(ostream& o,const person& x) { return o<<x.name<<':'<<x.age<<" "; } int main() { multiset<person> mp; mp.insert(person("ccc",16)); mp.insert(person("aaa",13)); mp.insert(person("aaa",13)); mp.insert(person("kkk",18)); mp.insert(person("fff",15)); mp.insert(person("eee",11)); mp.insert(person("jjj",16)); print(mp.begin(),mp.end()); multiset<person>::iterator it=mp.find(person("fff",15)); if(it==mp.end()) cout<<"not find!\n"; else { cout<<"find:"<<*it <<" "<<mp.count(*it)<<"个\n"; } person a("aaa",13); cout<<a<<" "<<mp.count(a)<<"个\n"; cout<<"lower/upper bound方法:\n"; multiset<person>::iterator ibegin,iend; ibegin=mp.lower_bound(a); iend=mp.upper_bound(a); print(ibegin,iend); cout<<"pair方法:\n"; typedef multiset<person>::iterator myiter;//给长类型起个别名 pair<myiter,myiter> p=mp.equal_range(a); print(p.first,p.second); cout<<"删除后输出:\n"; mp.erase(person("kkk",18));//有多个就删除多个 print(mp.begin(),mp.end()); }
结果:
相关文章推荐
- linux下练习 c++ 类库中list的特性、关联式容器共性介绍
- linux下练习 c++ 关联式容器map特性
- linux下练习 c++ 序列容器的使用
- linux下练习 c++ 特殊容器、特殊函数的使用
- linux下练习 c++ 普通容器的使用
- linux下练习 c++ 关联式容器multimap特性
- c++ 提高4 map容器 共性机制 使用时机 比较| STL算法 算法基础仿函数 谓词 函数适配器 遍历算法
- linux下练习 c++ 容器的vector的特性
- Google C++测试单元工具 gtest简单使用(win vs & linux)
- 用VS2012做MFC ActiveX控件并使用html、c#、c++测试容器
- linux下练习 c++ 库函数排序使用举例
- Linux下C++性能测试工具valgrind的使用
- 用VS2012做MFC ActiveX控件并使用html、c#、c++测试容器
- c++ STL关联式容器的共性(权哥)
- linux下练习 c++ 容器的deque的特性
- Linux c++ 测试框架 gtest使用简介
- 硬实时Linux(RT-Preempt Patch)在PC上的编译、使用和测试
- linux下简单vim命令的练习使用
- Linux上使用Qt Creator进行C/C++开发
- C++文本查询程序 不要定义类和智能指针管理数据 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果