由于这部分代码很多,每个容器的子代码,我就放在一起,不分类了。
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
#include<deque>
#include<ctime>
#include<stack>
#include<queue>
#include<list>
#include<set>
#include<map>
STL
1. vector容器 第一部分
/**************************vector容器存放内置数据类型********************************************/
//vector容器存放内置数据类型
void myprint(int val)
{
cout << val << endl;
}
void test01()
{
//创建一个vector容器,数组
vector<int>v;
//向容器中插入数据,
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
第一种遍历方式
通过迭代器访问容器中的数据
//vector<int>::iterator itBegin = v.begin();//起始迭代器,指向容器的第一个元素
//vector<int>::iterator itEnd = v.end();//结束迭代器,指向容器中的最后一个元素的下一个位置
//
//while (itBegin != itEnd)
//{
// cout << *itBegin << endl;//迭代器可以当指针用,可以解引用
// itBegin++;
//}
//第二种遍历方式 for
//for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
//{
// cout << *it << endl;
//}
//第三种遍历方式 利用STL中提供的遍历算法,记得加头文件
for_each(v.begin(), v.end(), myprint);
}
int main()
{
test01();
system("pause");
return 0;
}
/***********************vector存放自定义的数据类型*******************************/
class person
{
public:
person(string name,int age)
{
this->m_name = name;
this->m_age = age;
}
string m_name;
int m_age;
};
void test01()
{
vector<person>v;
person p1("aaa", 10);
person p2("bbb", 20);
person p3("ccc", 30);
person p4("ddd", 40);
person p5("eee", 50);
//向容器中添加数据
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
//遍历容器中的数据
for (vector<person>::iterator it = v.begin(); it != v.end(); it++)
{
cout <<"姓名:"<< (*it).m_name << endl;
cout << "姓名:" << it->m_name<< endl;//两者都行
}
}
//存放自定义类型的指针
void test02()
{
vector<person*>v;//加了*,存的是p1等的地址
person p1("aaa", 10);
person p2("bbb", 20);
person p3("ccc", 30);
person p4("ddd", 40);
person p5("eee", 50);
//向容器中添加数据
v.push_back(&p1);
v.push_back(&p2);
v.push_back(&p3);
v.push_back(&p4);
v.push_back(&p5);
//遍历容器
for (vector<person*>::iterator it = v.begin(); it != v.end(); it++)
{
cout << "姓名:" << (*it)->m_name << endl;
}
}
int main()
{
/*test01();*/
test02();
system("pause");
return 0;
}
/***************************************vector容器嵌套容器****************************************/
学习目标:容器中嵌套容器,我们将所有数据进行遍历输出
void test01()
{
vector<vector<int>>v;
//创建小容器
vector<int>v1;
vector<int>v2;
vector<int>v3;
vector<int>v4;
//向小容器中添加数据
for (int i = 0; i < 4; i++)
{
v1.push_back(i+1);
v2.push_back(i + 2);
v3.push_back(i + 3);
v4.push_back(i + 4);
}
//将小容器插入到大的容器中
v.push_back(v1);
v.push_back(v2);
v.push_back(v3);
v.push_back(v4);
//通过大容器,把所有的数据遍历一遍
for (vector<vector<int>>::iterator it = v.begin(); it != v.end(); it++)
{
//这里*it就是容器vector<int>
for (vector<int>::iterator vit = (*it).begin(); vit != (*it).end(); vit++)
{
cout << *vit << endl;
}
}
}
int main()
{
system("pause");
return 0;
}
2 string容器
/*******************************string基本概念************************************/
//string的构造函数
//1.string();创建一个空的字符串
//2.string(const char* s);使用字符串s初始化
//3.string(const string& str);使用一个string对象初始化另一个对象
//4.string(int n,char c);使用n个字符c初始化
void test01()
{
string s1;//1.默认构造
const char* str = "hello world!";
string s2(str);
cout <<"s2="<< s2 << endl;
string s3(s2);//拷贝构造
cout << "s3=" << s3 << endl;
string s4(10, 'a');
cout << "s4=" << s4 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
/*********************string赋值操作******************************/
void test01()
{
//operator
string str1;
str1 = "hello world";
cout << "str1=" << str1<< endl;
string str2;
str2 = str1;
cout << "str2=" << str2<<endl;
string str3;
str3 = 'a';
cout << "str3=" << str3 << endl;
//assign
string str4;
str4.assign("hello world");
cout << "str4=" << str4<< endl;
string str5;
str5.assign("hello world",5);//把前五个字符赋值
cout << "str5=" << str5 << endl;
string str6;
str6.assign(str5);
cout << "str6=" << str6<< endl;
string str7;
str7.assign(10, 'a');
cout << "str7=" << str7 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
/******************strng字符串拼接*************************/
void test01()
{
string str1 = "我";
str1 += "爱学习";
cout << "str1=" << str1 << endl;
str1 += ':';
cout << "str1=" << str1 << endl;
string str2 = "c++";
str1 += str2;
cout << "str1=" << str1 << endl;
string str3 = "I ";
str3.append("love study ");
cout << "str3=" << str3 << endl;
str3.append("c++ linux",3);//连接前三个字符
cout << "str3=" << str3 << endl;
str3.append(str2);
cout << "str3=" << str3 << endl;
str3.append(str2,1,2);
cout << "str3=" << str3 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
/***********************string查找和替换*******************************/
//int find(const string& str,int pos=0)const,默认为0
//查找
void test01()
{
string str1 = "abcdefgab";
int pos=str1.find("ab");
cout << "pos=" << pos << endl;//等于3 从0开始
int pos1 = str1.find("df");
cout << "pos1=" << pos1 << endl;//等于-1.没有
//rfind
pos = str1.rfind("ab");
cout << "pos=" << pos << endl;
}
//替换
//三个参数(int pos,int n,字符串)
void test02()
{
string str2 = "abcdefg";
str2.replace(0,3,"xyz");
cout << "str2=" << str2 << endl;//变成xyzdefg
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}
/***********************字符串比较******************************/
void test01()
{
string str1 = "abcde";
int c=str1.compare("ac");//abcde和ac进行比较,ac大
cout << c <<endl;
}
int main()
{
test01();
system("pause");
return 0;
}
/***********************字符存取******************************/
//通过[]
//通过at
void test01()
{
//[]
string str1 = "hello";
for (int i = 0; i < str1.size(); i++)
{
cout << str1[i] << endl;
}
//at
for (int i = 0; i < str1.size(); i++)
{
cout << str1.at(i) << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
/***********************字符串插入和删除******************************/
void test01()
{
string str1 = "hello";
str1.insert(0, "lbj");//插入
cout << str1 << endl;//lbjhello
str1.erase(0, 3);
cout << str1 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
/***********************string字串获取******************************/
void test01()
{
string str1 = "hello";
string str2=str1.substr(1, 2);
cout << str2 << endl;//结果为:el
}
//实用操作,找到email中的姓名
void test02()
{
string email = "zhangsan@qq.com";
int pos = email.find('@');
string name = email.substr(0, pos);
cout << "name is:" << name << endl;
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}
3.vector容器第二部分
/功能:创建vector容器
void printvector(vector<int>&v)
{
for (vector<int>::iterator it = v.begin(); it != v.end();it++)
{
cout << *it << " "<<endl;
}
cout << endl;
}
void test01()
{
vector<int>v1;//默认构造
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
printvector(v1);
//通过区间方式构造
vector<int>v2(v1.begin(), v1.end());
printvector(v2);
//n个elem方式构造(n,elem)
vector<int>v3(10, 1);
printvector(v3);
//拷贝构造函数
vector<int>v4(v3);
printvector(v4);
}
int main()
{
test01();
//test02();
system("pause");
return 0;
}
/*************************vector赋值操作*****************************/
void printvector(vector<int>&v)
{
for (vector<int>::iterator it = v.begin(); it != v.end();it++)
{
cout << *it << " "<<endl;
}
cout << endl;
}
void test01()
{
vector<int>v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
printvector(v1);
//赋值
vector<int>v2;
v2 = v1;
printvector(v2);
vector<int>v3;
v3.assign(v1.begin(), v1.end());
printvector(v3);
//(n,elem)
vector<int>v4;
v4.assign(10, 1);
printvector(v4);
}
int main()
{
test01();
//test02();
system("pause");
return 0;
}
/*************************vector容量和大小************************************/
void printvector(vector<int>&v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
vector<int>v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
printvector(v1);
if (v1.empty())//若1,则为空
{
cout << "v1为空" << endl;
}
else
{
cout << "v1不为空" << endl;
cout << "v1的容量为:"<<v1.capacity() << endl;
cout << "v1的元素个数为:" << v1.size() << endl;
}
v1.resize(15);//少了元素默认用0填充
printvector(v1);
}
int main()
{
test01();
//test02();
system("pause");
return 0;
}
/****************************vector插入和删除*************************************/
void printvector(vector<int>&v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
vector<int>v1;
//尾部插入
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(40);
v1.push_back(50);
printvector(v1);
//删除
v1.pop_back();
printvector(v1);
//指定插入
v1.insert(v1.begin(), 100);//默认在头插一个
printvector(v1);
v1.insert(v1.begin(),2, 100);//插两个
printvector(v1);
v1.insert(v1.begin()+1, 3, 1000);//在第二个位置插上三个1000
printvector(v1);
v1.erase(v1.begin(), v1.end());//清空
printvector(v1);
v1.clear();//这也是清空
printvector(v1);
}
int main()
{
test01();
//test02();
system("pause");
return 0;
}
/******************vector数据存取***************************************/
void printvector(vector<int>&v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
vector<int>v1;
//尾部插入
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(40);
v1.push_back(50);
printvector(v1);
int c=v1.at(1);
cout <<"c="<< c << endl;
int d = v1.front();
cout << "d=" << d << endl;
}
int main()
{
test01();
//test02();
system("pause");
return 0;
}
/*************************vector 互换容器******************************************/
//功能:实现两个容器内元素互换
void printvector(vector<int>&v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
vector<int>v1;
//尾部插入
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
cout << "互换前" << endl;
printvector(v1);
vector<int>v2;
for (int i = 10; i >0; i--)
{
v2.push_back(i);
}
printvector(v2);
cout << "互换后" << endl;
v2.swap(v1);
printvector(v1);
printvector(v2);
}
//实际用途
//巧用swap可以收缩内存
void test02()
{
vector<int>v;
for (int i = 0; i < 100000; i++)
{
v.push_back(i);
}
cout << "v的容量:" << v.capacity() << endl;
cout << "v的大小:" << v.size() << endl;
v.resize(3);//重新指定大小
cout << "v的容量:" << v.capacity() << endl;//容量不变
cout << "v的大小:" << v.size() << endl;
//巧用swap可以收缩内存
vector<int>(v).swap(v);
cout << "v的容量:" << v.capacity() << endl;//容量不变
cout << "v的大小:" << v.size() << endl;
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}
/************************************vector预留空间***********************************/
//reserve(int len)
void test01()
{
vector<int>v;
//利用reserve预留空间
v.reserve(100000);
int num = 0;
int *p = NULL;
for (int i = 0; i < 100000; i++)
{
v.push_back(i);
if (p != &v[0])
{
p = &v[0];
num++;//统计分配了多少次内存,不预留空间,为30,预留空间,为1
}
}
cout << "num=" << num<<endl;
}
int main()
{
test01();
//test02();
system("pause");
return 0;
}
4.deque容器
void printdeque(deque<int>&d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " " ;
}
cout << endl;
}
void test01()
{
deque<int>d1;
for (int i = 0; i < 10; i++)
{
d1.push_back(i);
}
printdeque(d1);
deque<int>d2(d1.begin(), d1.end());
printdeque(d2);
deque<int>d3(10,2);
printdeque(d3);
deque<int>d4(d3);
printdeque(d4);
}
int main()
{
test01();
//test02();
system("pause");
return 0;
}
/*********************************deque容器赋值操作**********************************/
//和vector一样
void printdeque(deque<int>&d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " " ;
}
cout << endl;
}
void test01()
{
deque<int>d1;
for (int i = 0; i < 10; i++)
{
d1.push_back(i);
}
printdeque(d1);
deque<int>d2;
d2.assign(d1.begin(), d1.end());
printdeque(d2);
deque<int>d3;
d3.assign(10,1);
printdeque(d3);
}
int main()
{
test01();
//test02();
system("pause");
return 0;
}
/*********************************deque容器大小操作*****************************************************/
void printdeque(deque<int>&d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " " ;
}
cout << endl;
}
void test01()
{
deque<int>d1;
for (int i = 0; i < 10; i++)
{
d1.push_back(i);
}
printdeque(d1);
if (d1.empty())
{
cout << "d1为空" << endl;
}
else
{
cout << "d1不为空" << endl;
}
int size = d1.size();
cout << "d1的大小" << size << endl;
d1.resize(5);
printdeque(d1);//重新指定大小
d1.resize(15,8);
printdeque(d1);//重新指定大小
}
int main()
{
test01();
//test02();
system("pause");
return 0;
}
/**********************deque的插入和删除***************************************/
void printdeque(deque<int>&d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " " ;
}
cout << endl;
}
void test01()
{
deque<int>d1;
d1.push_back(10);
d1.push_back(20);
d1.push_back(30);
d1.push_back(40);
d1.push_back(50);
d1.push_front(100);
d1.push_front(200);
printdeque(d1);
d1.pop_back();
printdeque(d1);
d1.insert(d1.begin(),1000);
printdeque(d1);
d1.insert(d1.begin(),2, 10000);
printdeque(d1);
d1.erase(d1.begin()+2);
printdeque(d1);
d1.clear();
printdeque(d1);
}
int main()
{
test01();
//test02();
system("pause");
return 0;
}
/************************deque容器数据存取*******************************/
void printdeque(deque<int>&d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " " ;
}
cout << endl;
}
void test01()
{
deque<int>d1;
for (int i = 0; i < 10; i++)
{
d1.push_back(i);
}
printdeque(d1);
cout << "第三个元素:"<< d1.at(3) << endl;
cout << "第一个元素:" << d1.front() << endl;
cout << "最后一个元素:" << d1.back() << endl;
}
int main()
{
test01();
//test02();
system("pause");
return 0;
}
/******************************deque容器排序操作***********************************************/
void printdeque(deque<int>&d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
deque<int>d1;
d1.push_back(10);
d1.push_back(20);
d1.push_back(30);
d1.push_back(40);
d1.push_back(50);
d1.push_front(100);
d1.push_front(200);
printdeque(d1);
sort(d1.begin(),d1.end());//记得包含算法的头文件algorithm
printdeque(d1);
}
int main()
{
test01();
//test02();
system("pause");
return 0;
}
5.案例-评委打分
//有5的选手,ABCDE,10个评委对每个人打分,去掉一个最高分和最低分,取平均值
//选手类
class person
{
public:
person(string name, int score)
{
this->m_name = name;
this->m_score = score;
}
string m_name;//姓名,
int m_score;//平均分
};
void createperson(vector<person>&v)
{
string nameseed= "ABCDE";
for (int i = 0; i < 5; i++)
{
string name = "选手";
name += nameseed[i];
int score = 0;
person p(name, score);
//将创建的person对象放入到容器中
v.push_back(p);
}
}
void setscore(vector<person>&v)
{
for (vector<person>::iterator it = v.begin(); it != v.end(); it++)
{
//将评委的分数放入到deque容器中
deque<int>d;
for (int i = 0; i < 10; i++)
{
int score = rand() % 41 + 60; //60-100
d.push_back(score);
}
//cout << "选手:" << it->m_name << "的打分:" << endl;
//for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++)
//{
// cout << *dit << " ";
//}
//cout << endl;
//排序
sort(d.begin(), d.end());//从小到大
//去除最高分和最低分
d.pop_back();
d.pop_front();
//取平均分
int sum = 0;
for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++)
{
sum += *dit;
}
int avg = sum / d.size();
//将平均分赋值给选手
it->m_score = avg;
}
}
void showscore(vector<person>&v)
{
for (vector<person>::iterator it = v.begin(); it != v.end(); it++)
{
cout << "姓名:" << (*it).m_name << " 分数" << (*it).m_score << endl;
}
cout << endl;
}
int main()
{
srand((unsigned int)time(NULL));
//1.创建五个选手
vector<person>v;
createperson(v);
//测试
//for (vector<person>::iterator it = v.begin(); it!= v.end(); it++)
//{
// cout << (*it).m_name <<"分数:"<<(*it).m_score<< endl;
//}
//2.给五个选手打分
setscore(v);
//3.显示得分
showscore(v);
//test02();
system("pause");
return 0;
}
6.stack容器
//前进后出
//pop
//push
//top
//
void test01()
{
stack<int>s;
s.push(10);
s.push(20);
s.push(30);
s.push(40);
cout << s.top() << endl;//栈顶元素
cout << s.size() << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
7.queue容器
//队列
//先进先出
//pop
//push
class person
{
public:
person(string name, int age)
{
this->m_name = name;
this->m_age = age;
}
string m_name;
int m_age;
};
void test01()
{
//创建队列
queue<person>q;
person p1("张三", 12);
person p2("李四", 14);
person p3("王五", 16);
person p4("赵六", 19);
//入队
q.push(p1);
q.push(p2);
q.push(p3);
q.push(p4);
cout <<"对头:"<< q.front().m_name << "年龄:"<<q.front().m_age<<endl;
cout << "对尾:" << q.back().m_name << "年龄:" << q.back().m_age << endl;
q.pop();
cout << "对头:" << q.front().m_name << "年龄:" << q.front().m_age << endl;
cout << "对尾:" << q.back().m_name << "年龄:" << q.back().m_age << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
8.list容器
链表
void printlist(list<int>&l)
{
for (list<int>::const_iterator it = l.begin(); it != l.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
list<int>l1;
//添加数据
l1.push_back(10);
l1.push_back(20);
l1.push_back(30);
l1.push_back(40);
printlist(l1);
list<int>l2(l1.begin(), l1.end());
printlist(l2);
list<int>l3(l2);
printlist(l3);
}
int main()
{
test01();
system("pause");
return 0;
}
/****************************list赋值和交换***************************************/
void printlist(list<int>&l)
{
for (list<int>::const_iterator it = l.begin(); it != l.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
list<int>l1;
//添加数据
l1.push_back(10);
l1.push_back(20);
l1.push_back(30);
l1.push_back(40);
printlist(l1);
list<int>l2;
l2.assign(l1.begin(), l1.end());
printlist(l2);
list<int>l3;
l3.assign(10, 2);
printlist(l3);
l3.swap(l2);
printlist(l3);
printlist(l2);
}
int main()
{
test01();
system("pause");
return 0;
}
/***********************************list大小操作********************************/
void printlist(list<int>&l)
{
for (list<int>::const_iterator it = l.begin(); it != l.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
list<int>l1;
//添加数据
l1.push_back(10);
l1.push_back(20);
l1.push_back(30);
l1.push_back(40);
printlist(l1);
cout << l1.size() << endl;
l1.resize(10);
printlist(l1);
}
int main()
{
test01();
system("pause");
return 0;
}
/************************list插入和删除***********************************/
void printlist(list<int>&l)
{
for (list<int>::const_iterator it = l.begin(); it != l.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
list<int>l1;
//添加数据
l1.push_back(10);
l1.push_back(20);
l1.push_back(30);
l1.push_back(40);
printlist(l1);
l1.pop_back();
printlist(l1);
list<int>::iterator it = l1.begin();
l1.insert(++it,2, 100);
printlist(l1);
l1.remove(100);
printlist(l1);
}
int main()
{
test01();
system("pause");
return 0;
}
/******************************list数据存取****************************************/
void printlist(list<int>&l)
{
for (list<int>::const_iterator it = l.begin(); it != l.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
list<int>l1;
//添加数据
l1.push_back(10);
l1.push_back(20);
l1.push_back(30);
l1.push_back(40);
printlist(l1);
cout << l1.front() << endl;
cout << l1.back()<< endl;
list<int>::iterator it = l1.begin();
cout << (*it) << endl;
it++;
//不能it=it+1,不支持随机访问
cout << (*it) << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
/*************************list反转和排序******************************************/
void printlist(list<int>&l)
{
for (list<int>::const_iterator it = l.begin(); it != l.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
bool mycompare(int v1,int v2)
{
return v1 > v2;
}
void test01()
{
list<int>l1;
//添加数据
l1.push_back(10);
l1.push_back(20);
l1.push_back(30);
l1.push_back(40);
l1.push_front(100);
l1.push_front(200);
l1.push_front(300);
l1.push_front(400);
printlist(l1);
l1.reverse();//反转
printlist(l1);
l1.sort();//排序
printlist(l1);
l1.sort(mycompare);//降序
printlist(l1);
}
int main()
{
test01();
system("pause");
return 0;
}
/**************************list排序案例**************************************/
将person自定义数据类型进行排序,person中属性有姓名,年龄,身高
排序规则:按照年龄进行排序,如果年龄相同,则按照身高排序
class person
{
public:
person(string name, int age, int high)
{
this->m_name = name;
this->m_age = age;
this->m_high = high;
}
string m_name;
int m_age;
int m_high;
};
void createperson(list<person>&L)
{
string nameseed = "ABCDE";
for (int i = 0; i < 5; i++)
{
string name = "姓名";
name += nameseed[i];
int age = rand() % 6 + 15;//15-20
int high = rand() % 11 + 170;//170-180
person p(name, age,high);
L.push_back(p);
}
cout << "排序前:" << endl;
for (list<person>::iterator it = L.begin(); it != L.end(); it++)
{
cout << it->m_name << ": 年龄:" << it->m_age << " 身高:" << it->m_high << endl;
}
}
bool compareperson(person &p1,person&p2)
{
if (p1.m_age < p2.m_age)
{
return true;
}
else if (p1.m_age > p2.m_age)
{
return false;
}
else
{
if (p1.m_high < p2.m_high)
{
return true;
}
else if (p1.m_high > p2.m_high)
{
return false;
}
}
//或者
//if (p1.m_age == p2.m_age)
//{
// return p1.m_high < p2.m_high;
//}
//else
//{
// return p1.m_age < p2.m_age;
//}
}
void sortperson(list<person>&L)
{
list<person>::iterator it = L.begin();
L.sort(compareperson);
cout << "排序后:" << endl;
for (list<person>::iterator it = L.begin(); it != L.end(); it++)
{
cout << it->m_name << ": 年龄:" << it->m_age << " 身高:" << it->m_high << endl;
}
}
int main()
{
srand((unsigned int)time(NULL));
list<person>L;//创建一个list容器L
//给学生的姓名年龄身高赋值
createperson(L);
//排序
sortperson(L);
system("pause");
return 0;
}
9.set容器
set会自动排序
void printset(set<int>&s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it <<" ";
}
cout << endl;
}
void test01()
{
set<int>s1;
s1.insert(10);
s1.insert(20);
s1.insert(40);
s1.insert(30);
s1.insert(40);
//set会自动排序
printset(s1);//set不能有重复的,会去掉
set<int>s2(s1);
printset(s2);
}
int main()
{
test01();
system("pause");
return 0;
}
/*********************set大小操作********************/
void printset(set<int>&s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
set<int>s1;
s1.insert(10);
s1.insert(20);
s1.insert(40);
s1.insert(30);
s1.insert(40);
//set会自动排序
printset(s1);//set不能有重复的,会去掉
cout <<"容器大小:"<<s1.size() << endl;
set<int>s2;
s2.insert(10);
s2.insert(20);
s2.insert(30);
printset(s2);
//交换
s2.swap(s1);
printset(s2);
}
int main()
{
test01();
system("pause");
return 0;
}
/************************set插入和删除********************************/
void printset(set<int>&s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
set<int>s1;
s1.insert(10);
s1.insert(20);
s1.insert(40);
s1.insert(30);
s1.insert(40);
//set会自动排序
printset(s1);//set不能有重复的,会去掉
s1.erase(s1.begin());
printset(s1);
s1.clear();
printset(s1);
}
int main()
{
test01();
system("pause");
return 0;
}
/************************set查找和统计****************************/
void printset(set<int>&s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
set<int>s1;
s1.insert(10);
s1.insert(20);
s1.insert(40);
s1.insert(30);
s1.insert(40);
//set会自动排序
printset(s1);//set不能有重复的,会去掉
set<int>::iterator pos = s1.find(30);
if (pos != s1.end())
{
cout << "找到元素" << endl;
}
else{
cout << "没找到元素" << endl;
}
cout << s1.count(30) << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
/***************************set和multiset的区别************************************/
//set不能插同样的值
//multiset能插同样的值
void printset(set<int>&s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void printmultiset(multiset<int>&s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
set<int>s1;
pair<set<int>::iterator, bool>ret = s1.insert(10);
if (ret.second)
{
cout << "第一次插入成功" << endl;
}
else
{
cout << "第一次插入失败" << endl;
}
ret = s1.insert(10);
if (ret.second)
{
cout << "第二次插入成功" << endl;
}
else
{
cout << "第二次插入失败" << endl;
}
s1.insert(10);
s1.insert(20);
s1.insert(40);
s1.insert(30);
s1.insert(40);
//set会自动排序
printset(s1);//set不能有重复的,会去掉
multiset<int>m1;
m1.insert(1);
m1.insert(2);
m1.insert(1);
m1.insert(3);
printmultiset(m1);
}
int main()
{
test01();
system("pause");
return 0;
}
/*************************pair队组创建********************************/
//set不能插同样的值
//multiset能插同样的值
void printset(set<int>&s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void printmultiset(multiset<int>&s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
//第一种方式
pair<string, int>p("tom",13);
cout << "姓名:" << p.first << " 年龄:" << p.second << endl;
//第二种方式
pair<string, int>p2 = make_pair("jerry", 20);
cout << "姓名:" << p2.first << " 年龄:" << p2.second << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
/****************************set容器-内置类型指定排序规则****************************************/
class compareset
{
public:
bool operator()(int v1,int v2)//重载()
{
return v1 > v2;
}
};
void test01()
{
set<int>s1;
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(40);
for (set<int>::iterator it = s1.begin(); it != s1.end(); it++)
{
cout << *it << " ";
}
cout << endl;
//指定从大到小
set<int, compareset>s2;//
s2.insert(10);
s2.insert(30);
s2.insert(20);
s2.insert(40);
for (set<int,compareset>::iterator it = s2.begin(); it != s2.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
/***********************set容器-自定义类型指定排序规则****************************/
class person
{
public:
person(string name, int age)
{
this->m_name = name;
this->m_age = age;
}
string m_name;
int m_age;
};
class compareperson
{
public:
bool operator()(const person&p1, const person&p2)
{
return p1.m_age > p2.m_age;
}
};
void test01()
{
set<person, compareperson>s;
//自动逸数据类型,得自己指定排序规则
person p1("张三", 24);
person p2("李四", 25);
person p3("王五", 29);
person p4("赵六", 22);
s.insert(p1);
s.insert(p2);
s.insert(p3);
s.insert(p4);
for (set<person, compareperson>::iterator it = s.begin(); it != s.end(); it++)
{
cout <<"姓名:" <<it->m_name<<" 年龄:"<<it->m_age << " ";
}
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
10. map/multimap容器
会按照key自动排序
map所有元素都是pair
第一个是key(键值),第二个是value(实值)
不允许插入同样的key
void printmap(map<int, int>&m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key=" << it->first << " value=" << it->second << endl;
}
}
void test01()
{
map<int, int>m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
m.insert(pair<int, int>(4, 40));
printmap(m);
map<int, int>m2(m);//拷贝构造
printmap(m2);
map<int, int>m3;
m3 = m2;
printmap(m3);
}
int main()
{
test01();
system("pause");
return 0;
}
/******************************map大小和交换***************************************************/
void printmap(map<int, int>&m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key=" << it->first << " value=" << it->second << endl;
}
}
void test01()
{
map<int, int>m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
m.insert(pair<int, int>(4, 40));
printmap(m);
cout << m.size() << endl;
map<int, int>m2;//拷贝构造
m2.insert(pair<int, int>(1, 1000));
m2.insert(pair<int, int>(2, 2000));
m2.insert(pair<int, int>(3, 3000));
m2.insert(pair<int, int>(4, 4000));
printmap(m2);
m2.swap(m);//交换
printmap(m2);
printmap(m);
}
int main()
{
test01();
system("pause");
return 0;
}
/************************************map插入和删除********************************************/
不允许插入同样的key
void printmap(map<int, int>&m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key=" << it->first << " value=" << it->second << endl;
}
}
void test01()
{
map<int, int>m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
m.insert(pair<int, int>(4, 40));
m.insert(make_pair(5, 50));//也是插入
printmap(m);
m.erase(m.begin());//删掉第一个
m.erase(3);//删掉key为3的
printmap(m);
}
int main()
{
test01();
system("pause");
return 0;
}
/**********************************map查找和统计*******************************************/
void printmap(map<int, int>&m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key=" << it->first << " value=" << it->second << endl;
}
}
void test01()
{
map<int, int>m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
m.insert(pair<int, int>(4, 40));
m.insert(make_pair(5, 50));//也是插入
printmap(m);
map<int, int>::iterator pos = m.find(3);
if (pos != m.end())
{
cout << "查到了元素key=" << pos->first<<" value="<<pos->second<< endl;
}
else{
cout << "没有查到元素" << endl;
}
//m.count(3);
cout << m.count(3) << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
/****************************map排序***************************************/
//利用仿函数,可以实现从大到小
class mycompare
{
public:
bool operator()(int v1, int v2)
{
//降序
return v1 > v2;
}
};
void printmap(map<int, int, mycompare>&m)
{
for (map<int, int, mycompare>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key=" << it->first << " value=" << it->second << endl;
}
}
void test01()
{
map<int, int, mycompare>m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
m.insert(pair<int, int>(4, 40));
m.insert(make_pair(5, 50));
printmap(m);
}
int main()
{
test01();
system("pause");
return 0;
}
11.案例—员工分组
10个员工:
员工信息:姓名,工资组成;部门分为:策划,美术,研发。
随机给员工分配部门和工资。
通过multimap进行信息的插入 key(部门编号),value(员工)。
分部门显示员工信息。
实现步骤:
1.10个员工放入vector
2.遍历vector容器,取出每个员工,进行随机分组。
3.分组后,将员工部门编号作为key,员工工作为value,放入到multimap中。
4.分部门显示员工信息。
#define CEHUA 0
#define MEISHU 1
#define YANFA 2
class worker
{
public:
worker(string name, int price)
{
this->m_name = name;
this->m_price = price;
}
string m_name;
int m_price;
};
void createworker(vector<worker>&v)
{
string nameseed = "ABCDEFGHIJ";
for (int i = 0; i < 10; i++)
{
string name = "员工";
name += nameseed[i];
int price = rand() % 500 + 5000;
worker w(name, price);
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 deptid = rand() % 3;//0,1,2
//将员工插入到分组中,key是部门编号,value是具体员工
m.insert(make_pair(deptid, *it));
}
}
//显示分组员工信息
void showworkerbygroup(multimap <int, worker>&m)
{
cout << "策划部门:" << endl;
multimap<int, worker>::iterator pos = m.find(CEHUA);
int count = m.count(CEHUA);
int index = 0;
for (; pos != m.end() && index < count; pos++, index++)
{
cout << "姓名;" << pos->second.m_name << " 工资" << pos->second.m_price << endl;
}
cout << "----------------------------------------" << endl;
cout << "美术部门:" << endl;
pos = m.find(MEISHU);
count = m.count(MEISHU);
index = 0;
for (; pos != m.end() && index < count; pos++, index++)
{
cout << "姓名;" << pos->second.m_name << " 工资" << pos->second.m_price << endl;
}
cout << "----------------------------------------" << endl;
cout << "研发部门:" << endl;
pos = m.find(YANFA);
count = m.count(YANFA);
index = 0;
for (; pos != m.end() && index < count; pos++, index++)
{
cout << "姓名;" << pos->second.m_name << " 工资" << pos->second.m_price << endl;
}
}
int main()
{
//1.创建员工
vector<worker>vworker;
createworker(vworker);
//2.员工分组
multimap <int, worker>mworker;
setgroup(vworker,mworker);
//3.分组显示员工
showworkerbygroup(mworker);
//测试
cout << "----------------------------------" << endl;
cout << "所有员工" << endl;
for (vector<worker>::iterator it = vworker.begin(); it != vworker.end(); it++)
{
cout << "姓名:" << it->m_name << " 年龄" << it->m_price << endl;
}
system("pause");
return 0;
}