3 常用容器
3.4 stack容器
3.5 queue容器
3.6 list容器
class Person {
public:
Person(string name,int age, int height) {
this->name = name;
this->age = age;
this->height = height;
}
string name;
int age;
int height;//身高
};
//person排序规则,如果年龄相同,按照身高从大到小
bool myCompare(Person& p1, Person& p2) {
if (p1.age == p2.age)
return p1.height > p2.height;
return p1.age > p2.age;
}
void printList(list<Person>& l) {
for (list<Person>::iterator it = l.begin(); it != l.end(); ++it) {
cout << "姓名: " << (*it).name << " 年龄: " << (*it).age << " 身高: " << (*it).height << endl;
}
}
void test() {
list<Person> L;
Person p1("C++", 20,175);
Person p2("C", 23,179);
Person p3("Python", 11,166);
Person p4("Java", 19,187);
Person p5("GO", 19, 180);
Person p6("PHP", 20, 177);
L.push_back(p1);
L.push_back(p2);
L.push_back(p3);
L.push_back(p4);
L.push_back(p5);
L.push_back(p6);
//所有不支持随机访问的迭代器,容器内部都会提供排序算法
L.sort(myCompare);
printList(L);
}
3.7set容器
void test() {
set<int>s;
s.insert(5);
s.insert(1);
s.insert(9);
s.insert(3);
s.insert(7);
set<int>::iterator pos = s.find(3);
//判断是否找到
if (pos != s.end())
cout << "找到了,值为: " << *pos << endl;
else
cout << "未找到" << endl;
}
void test() {
set<int>s;
s.insert(5);
s.insert(1);
s.insert(9);
s.insert(3);
s.insert(7);
//equal_range(keyElem)返回容器中key与keyElem相等的上下限的两个迭代器
//上下限为lower_bound和upper_bound
pair<set<int>::iterator, set<int>::iterator> ret = s.equal_range(3);
//获取第一个值
if (ret.first != s.end())
cout << "找到equal_range中lower_bound的值:" << (*ret.first) << endl;
else
cout << "未找到" << endl;
//获取第二个值
if (ret.second != s.end())
cout << "找到equal_range中upper_bound的值:" << *(ret.second) << endl;
else
cout << "未找到" << endl;
}
//创建对组
void test() {
//第一种
pair<string, int> p(string("Tom"), 100);
//取值
cout << "姓名:" << p.first << endl;
cout << "年龄:" << p.second << endl;
//第二种
pair<string, int> p2 = make_pair("Jerry", 200);
cout << "姓名:" << p2.first << endl;
cout << "年龄:" << p2.second << endl;
}
//指定set排序规则 从大到小
//仿函数
class myCompare {
public:
//重载()
bool operator()(int v1, int v2)const {
return v1 > v2;
}
};
void test() {
//从大到小排序,在插入前就指定排序规则
set<int, myCompare>s;
s.insert(5);
s.insert(1);
s.insert(9);
s.insert(3);
s.insert(7);
for (set<int, myCompare>::iterator it = s.begin(); it != s.end(); ++it) {
cout << *it << " ";
}
}
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stdexcept>
#include <deque>
#include <ctime>
#include <list>
#include <set>
using namespace std;
//自定义数据类型的排序
class Person {
public:
Person(string name, int age) {
this->name = name;
this->age = age;
}
string name;
int age;
};
//仿函数
class myCompare
{
public:
};
bool operator < (const Person& p1, const Person& p2) {
return p1.age > p2.age;
}
void test() {
//插入自定义数据类型时,需要在开始时指定排序规则
set<Person>s;
Person p1("C++", 20);
Person p2("C", 23);
Person p3("Python", 11);
Person p4("Java", 19);
s.insert(p1);
s.insert(p2);
s.insert(p3);
s.insert(p4);
//显示
for (set<Person, myCompare>::iterator it = s.begin(); it != s.end(); ++it) {
cout << "姓名:" << (*it).name << "年龄:" << (*it).age << endl;
}
}
int main(){
test();
return 0;
}
3.8 map容器
//指定set排序规则 从大到小
//仿函数
class myCompare {
public:
//重载()
bool operator()(int v1, int v2) const{
return v1 > v2;
}
};
void test() {
//从大到小排序
map<int, int,myCompare>m;
m.insert(pair<int, int>(1, 10));
m.insert(make_pair(2, 20));
m.insert(map<int, int>::value_type(3, 30));
m[4] = 40;
for (map<int, int, myCompare>::iterator it = m.begin(); it != m.end(); ++it) {
cout << "key: " << it->first << " value: " << (*it).second << endl;
}
}
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stdexcept>
#include <deque>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
enum{RENLI,YANFA,MEISHU};
class Worker {
public:
string name;
int money;
};
void createWorker(vector<Worker>& v) {
string nameSeed = "ABCDE";
for (int i = 0; i < 5; ++i) {
string name = "员工";
name += nameSeed[i];
int money = rand() % 10000 + 10000;//10000~19999
Worker w;
w.name = name;
w.money = money;
v.push_back(w);
}
}
void setGroup(vector<Worker>& v, multimap<int, Worker>& m) {
for (vector<Worker>::iterator it = v.begin(); it != v.end(); ++it) {
//随机产生部门编号,使用枚举定义
int departmentID = rand() % 3;//0 1 2
//将员工分到multimap容器中
m.insert(make_pair(departmentID, *it));
}
}
void showGroup(multimap<int, Worker>& m) {
//人力部门显示
cout << "人力部门员工如下:" << endl;
multimap<int, Worker>::iterator pos = m.find(RENLI);
int index = 0;
int num = m.count(RENLI);
for (; pos != m.end(), index < num; ++pos, index++) {
cout << "姓名: " << pos->second.name << " 工资: " << pos->second.money << endl;
}
cout << "------------------------------------" << endl;
cout << "研发部门员工如下:" << endl;
index = 0;
pos = m.find(YANFA);
num = m.count(YANFA);
for (; pos != m.end(), index < num; ++pos, index++) {
cout << "姓名: " << pos->second.name << " 工资: " << pos->second.money << endl;
}
cout << "------------------------------------" << endl;
cout << "美术部门员工如下:" << endl;
index = 0;
pos = m.find(MEISHU);
num = m.count(MEISHU);
for (; pos != m.end(), index < num; ++pos, index++) {
cout << "姓名: " << pos->second.name << " 工资: " << pos->second.money << endl;
}
}
void test() {
vector<Worker> v;
//创建5名员工
createWorker(v);
//设置分组
//分组的multimap
multimap<int, Worker> m;
setGroup(v, m);
//分部门显示员工
showGroup(m);
}
int main(){
test();
return 0;
}
3.9 各容器使用时机