STL关联容器简单使用
2018-04-05 23:00
337 查看
set
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<set> #include<time.h> #include<functional> using namespace std; //1.集合 元素唯一 自动排序 不能按照[]方式插入元素 //红黑树 void main91() { set<int> set1; for (int i = 0; i < 5; i++) { int temp = rand(); set1.insert(temp); } set1.insert(100); set1.insert(100); set1.insert(100); for (set<int>::iterator it = set1.begin(); it != set1.end(); it++) { cout << *it << " "; } while (!set1.empty()) { set<int>::iterator it = set1.begin(); cout << *it << endl; set1.erase(it); } } void main92() { set<int> set1; set<int, less<int>> set2;//默认便是从小到大排列 set<int, greater<int>> set3;//从大到小排列 for (int i = 0; i < 5; i++) { int tmp = rand(); set3.insert(tmp); } for (set<int, greater<int>>::iterator it = set3.begin(); it != set3.end(); it++)//迭代器的类型不要忘了仿函数哟! { cout << *it << " "; } } class Student { public: Student(char* name, int age) { strcpy(this->name, name); this->age = age; } public: char name[64]; int age; }; //仿函数的用法 //自定义类型数据的排序 struct FuncStudent { bool operator()(const Student&left,const Student&right) //重载了()类似于函数,所以称为仿函数 { if (left.age < right.age) return true; else return false; } }; void main93() { Student t1("t1", 15); Student t2("t2", 12); Student t3("t3", 16); Student t4("t4", 13); Student t5("t5", 15); set<Student, FuncStudent> set1; set1.insert(t1); set1.insert(t2); set1.insert(t3); set1.insert(t4); set1.insert(t5); for (set<Student, FuncStudent>::iterator it = set1.begin(); it != set1.end(); it++) { cout << it->age << " " << it->name << endl; } } //typedef pair(iterator,bool) pair是insert的返回值类型,pair译为对组 //如何判断inset的返回值 //pair的用法 void main94() { Student s1("s1", 16); Student s2("s5", 16); set<Student, FuncStudent> set1; pair<set<Student, FuncStudent>::iterator, bool> pair1 = set1.insert(s1); if (pair1.second) { cout << "s1插入成功!" << endl; } else cout << "s1插入失败!" << endl; pair<set<Student, FuncStudent>::iterator, bool> pair2 = set1.insert(s2); if (pair2.second) { cout << "s2插入成功!" << endl; } else cout << "s2插入失败!" << endl; } void main95() { set<int> set1; for (int i = 0; i < 7; i++) { set1.insert(i + 1); } for (set<int>::iterator it = set1.begin(); it != set1.end(); it++) { cout << *it << " "; } set<int>::iterator it0 = set1.find(5); cout << "元素为5的位置:" << *it0 << endl; int num = set1.count(5); cout << "元素5的数量:" << num << endl; set<int>::iterator it1 = set1.lower_bound(5); cout << "大于等于5的迭代器的位置:" << *it1 << endl; set<int>::iterator it2 = set1.upper_bound(5); cout << "大于5的迭代器的位置:" << *it2 << endl; pair<set<int>::iterator, set<int>::iterator> mypair = set1.equal_range(5);//返回容器中与elem相等的上下线的两个迭代器。上限是闭区间,下限是开区间,如[begin,end) set<int>::iterator it3 = mypair.first; cout << *it3 << endl; set<int>::iterator it4 = mypair.second; cout << *it4 << endl; } void main() { main95(); }
multiset
#include<iostream> #include<set> using namespace std; // multimap与map的区别:map支持唯一键值,每个键只能出现一次;而multimap中相同键可以出现多次。multimap不支持[]操作符。 void main1001() { multiset<int> set1; int tmp = 0; //为set1赋值 cin >> tmp; while (cin.get()) { set1.insert(tmp); cin >> tmp; if (tmp == 0) break; } for (multiset<int>::iterator it = set1.begin(); it != set1.end(); it++) { cout << *it << " "; } cout << endl; while (!set1.empty()) { multiset<int>::iterator it = set1.begin(); cout << *it << " "; set1.erase(it); } cout << endl; } void main() { main1001(); }
map
#include<iostream> #include<map> #include<string> using namespace std; void main1101() { map<int, string>map1; //第一种方法: map1.insert(pair<int, string>(1, "teacher01")); map1.insert(pair<int, string>(2, "teacher02")); //第二种方法: map1.insert(make_pair(3, "teacher03")); map1.insert(make_pair(4, "teacher04")); //第三种方法: map1.insert(map<int, string>::value_type(5, "teacher05")); map1.insert(map<int, string>::value_type(6, "teacher06")); //第四种方法: map1[7] = "teacher07"; map1[8] = "teacher08"; for (map<int, string>::iterator it = map1.begin(); it != map1.end(); it++) { cout << it->first << "\t" << it->second << endl; } while (!map1.empty()) { map<int, string>::iterator it = map1.begin(); cout << it->first << "\t" << it->second << endl; map1.erase(it); } } //插入的四种方法 异同 //前三种方法返回的都是pair<iterator,bool>若key已经存在则报错 //方法4 若key已经存在则修改 void main1102() { map<int, string>map1; //方法1 pair<map<int,string>::iterator,bool> mypair1 = map1.insert(pair<int, string>(1, "teacher01")); map1.insert(pair<int, string>(2, "teacher02")); //方法2 pair<map<int, string>::iterator, bool> mypair3 = map1.insert(make_pair(3, "teacher03")); map1.insert(make_pair(4, "teacher04")); //方法3 pair<map<int, string>::iterator, bool> mypair5 = map1.insert(map<int, string>::value_type(5, "teacher05")); if (mypair5.second) { cout << "key 5 插入成功!" << mypair5.first->first << endl; } else cout << "key 5 插入失败!" << endl; pair<map<int, string>::iterator, bool>mypair6 = map1.insert(map<int, string>::value_type(5, "teacher06")); if (mypair6.second) { cout << "key 5插入成功!" << mypair6.first->first << endl; } else { cout << "key 5插入失败!" << endl; } //第四种方法 map1[7] = "teacher07"; map1[7] = "teacher077"; for (map<int, string>::iterator it = map1.begin(); it != map1.end(); it++) { cout << it->first << "\t" << it->second << endl; } } void main1103() { map<int , string> map1; //第一种方法 map1.insert(pair<int, string>(1, "teacher01")); map1.insert(pair<int, string>(2, "teacher02")); //第二种方法 map1.insert(make_pair(3, "teacher03")); map1.insert(make_pair(4, "teacher04")); //第三种方法 map1.insert(map<int, string>::value_type(5, "teacher05")); map1.insert(map<int, string>::value_type(6, "teacher06")); //第四种方法 map1[7] = "teacher07"; map1[8] = "teacher08"; for (map<int, string>::iterator it = map1.begin(); it != map1.end(); it++) { cout << it->first << "\t" << it->second << endl; } //map的查找 map<int, string>::iterator it1 = map1.find(100); if (it1 == map1.end()) { cout << "key 100不存在!" << endl; } else cout << it1->first << "\t" << it1->second << endl; pair<map<int, string>::iterator, map<int, string>::iterator> mypair = map1.equal_range(5); if (mypair.first == map1.end()) { cout << "迭代器1 大于等于5的位置不存在" << endl; } else { cout << mypair.first->first << "\t" << mypair.first->second << endl; } if (mypair.second == map1.end()) { cout << "迭代器2 大于5的位置不存在" << endl; } else { cout << mypair.second->first << "\t" << mypair.second->second << endl; } } void main() { main1103(); }
multimap
#include<iostream> #include<string> #include<map> using namespace std; class Person { public: string name; int age; string tel; double saly; }; void main1201() { Person p1, p2, p3, p4, p5; p1.name = "王1"; p1.age = 31; p2.name = "王2"; p2.age = 32; p3.name = "王2"; p3.age = 33; p4.name = "张1"; p4.age = 34; p5.name = "张2"; p5.age = 35; multimap<string,Person> map1; map1.insert(make_pair("Sale", p1)); map1.insert(make_pair("Sale", p2)); map1.insert(make_pair("Sale", p3)); map1.insert(make_pair("development", p4)); map1.insert(make_pair("finance", p5)); for (multimap<string, Person>::iterator it = map1.begin(); it != map1.end(); it++) { cout << it->first << "\t" << it->second.name << endl; } int tag = 0; int num = map1.count("Sale"); cout << "销售部有" << num << "人" << endl; multimap<string, Person>::iterator it2 = map1.find("Sale"); while (it2 != map1.end() && tag < num) { cout << it2->first << "\t" << it2->second.name << endl; it2++; tag++; } } void main1202() { Person p1, p2, p3, p4, p5; p1.name = "王1"; p1.age = 31; p2.name = "王2"; p2.age = 32; p3.name = "张3"; p3.age = 33; p4.name = "张4"; p4.age = 34; p5.name = "张5"; p5.age = 35; multimap<string, Person> map2; map2.insert(make_pair("sale", p1)); map2.insert(make_pair("sale", p2)); map2.insert(make_pair("development", p3)); map2.insert(make_pair("development", p4)); map2.insert(make_pair("Financial", p5)); //按照条件检索数据,进行修改 for (multimap<string, Person>::iterator it = map2.begin(); it != map2.end(); it++) { if (it->second.age == 32) it->second.name = "name2"; } cout << "遍历结束" << endl; for (multimap<string, Person>::iterator it = map2.begin(); it != map2.end(); it++) { cout << it->first << "\t" << it->second.name << endl; } } void main() { main1202(); }
相关文章推荐
- STL的基本使用之关联容器:set和multiSet的基本使用
- 使用STL的经验总结_指定关联容器的比较类型_12
- C++中防止STL中迭代器失效——map/set等关联容器——vector/list/deque等序列容器—如何防止迭代器失效—即erase()的使用
- STL的基本使用之关联容器:map和multiMap的基本使用
- C++中防止STL中迭代器失效__map/set等关联容器vector/list/deque等序列容器_如何防止迭代器失效_即erase()的使用
- STL之关联容器(pair、map、set的使用)
- STL之关联容器(pair、map、set的使用)
- C++中防止STL中迭代器失效——map/set等关联容器——vector/list/deque等序列容器—如何防止迭代器失效—即erase()的使用
- C++中防止STL中迭代器失效__map/set等关联容器vector/list/deque等序列容器_如何防止迭代器失效_即erase()的使用
- C++之STL中常用关联容器使用方法
- 【STL容器学习】-关联容器与map的使用方法
- STL 之 C++ Map容器的简单使用方法
- STL::map关联容器
- STL容器erase的使用陷阱(三)
- STL容器使用DEMO-vector
- 使用 MAP CLASS 库创建关联容器
- STL容器之间可以直接相互赋值使用
- Flex3 快速入门:构建简单的用户界面 使用容器
- 关于在STL容器list中使用find_if 函数
- STL容器(二) 关联容器