目录
STL初识
/*c++的面向对象和泛型编程思想,目的就是复用性的提高
大多数ing况下,数据结构和算法都未能有一套标准,导致被迫从事大量重复工作
为了建立数据结构和算法的一套标准,诞生了STL
STL(Standdard Template Library)标准模板库
STL从广义上分为 容器(container) 算法(algorithm) 迭代器(iterator)
算法和容器之间通过迭代器无缝连接
STL几乎所有的代码都采用了模板或者模板函数
STL溜达组件 容器 算法 迭代器 仿函数 适配器(配接器)空间配置器
容器为各种数据结构 如 vector list deque set map
算法 sort find copy for_each
迭代器 扮演容器和算法的胶合剂
仿函数 行为类似函数 可作为算法的某种策略
适配器 一种用来修饰容器或者仿函数或迭代器接口的东西
空间配置器 负责空间的配置和管理*/
vector容器
vector存放内置数据类型
#include<iostream>
#include<vector>
#include<algorithm>//标准算法头文件
using namespace std;
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++;
}
cout << endl;
//第二种
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << endl;
}
//第三种 STL中提供的遍历算法
cout << endl;
for_each(v.begin(), v.end(), myPrint);
}
int main()
{
test01();
system("pause");
return 0;
}
vector存放自定义数据类型
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
//vector容器存放自定义数据类型
class Person
{
public:
Person(string Name, int Age)
{
m_Name = Name;
m_Age = Age;
}
string m_Name;
int m_Age;
};
void test01()
{
vector<Person>v;
Person p1("HRY", 10);
Person p2("HRY", 11);
Person p3("HRY", 12);
Person p4("HRY", 13);
Person p5("HRY", 14);
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 << "name:" << (*it).m_Name << " age:" << (*it).m_Age << endl;
}
}
void test02()//存放自定义类型 指针
{
vector<Person*>v;
Person p1("HRY", 10);
Person p2("HRY", 11);
Person p3("HRY", 12);
Person p4("HRY", 13);
Person p5("HRY", 14);
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 << "name:" << (*it) ->m_Name << " age:" << (*it)->m_Age << endl;
}
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
vector容器嵌套容器
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
//容器嵌套容器
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 << " ";
}
cout << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
插入和删除
#include<iostream>
#include<vector>
using namespace std;
/*
* push_back(ele); 尾部插入元素ele
* pop_back(); 删除最后一个元素
* insert(const_iterator pos, ele); 迭代器指向位置pos插入元素ele
* insert(const_iterator pos, int count, ele); 迭代器指向位置pos插入count个元素ele
* erase(const_iterator pos); 删除迭代器指向的元素
* erase(const_iterator start, const_iterator end); 删除迭代器从start到end之间的元素
* clear(); 删除容器中所有的元素
*/
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);
//尾删
v1.pop_back();
printVector(v1);
//插入
v1.insert(v1.begin(), 100);
printVector(v1);
v1.insert(v1.begin(), 2, 1000);
printVector(v1);
v1.erase(v1.begin());
printVector(v1);
v1.erase(v1.begin(), v1.end());
printVector(v1);
}
int main()
{
test01();
system("pause");
return 0;
}
赋值操作
#include<iostream>
#include<vector>
using namespace std;
/*
* vecotr& operator=(const vector &vec); 重载等号操作符
* assign(beg, end); 将[beg, end)区间中的数据拷贝赋值给本身
* assign(n,elem); 将n个elem拷贝赋值给本身
*/
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);
//赋值
vector<int>v2 = v1;
printVector(v1);
vector<int>v3;
v3.assign(v1.begin(), v1.end());
printVector(v3);
vector<int> v4;
v4.assign(10, 100);
printVector(v4);
}
int main()
{
test01();
system("pause");
return 0;
}
构造函数
#include<iostream>
#include<vector>
using namespace std;
/*
* vector数据结构和数组非常相似,也称为单端数组
* vector数组与普通数组区别
* 数组是静态空间vector可以动态扩展
* 动态扩展:并不是在原空间之后续接新空间,而是找更大的内存空间,然后将原数拷贝到新空间,释放原空间
* vector容器的迭代器是支持随机访问的迭代器
* rend指向只一个元素的前一个位置 begin指向第一个 rbegin指向最后一个 end指向最后一个位置的下一个
*/
/*
* vector<T>v; 采用类模板实现,默认构造函数
* vector(v.begin(), v.end()); 将v[begin(),end())区间中的函数拷贝给本身 区间前闭后开
* vector(n,elem); 构造函数将n哥哥elem拷贝给本身
* vector(const vector &vec); 拷贝构造函数
*/
void printVector(vector<int>& v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it<<" ";
}
}
void test01()
{
vector<int>v1;//默认构造函数 无参构造
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
for (int i = 0; i < 10; i++)
{
cout << v1[i]<<" ";
}
cout << endl;
printVector(v1);
cout << endl;
vector<int>v2(v1.begin(), --v1.end());
printVector(v2);
cout << endl;
vector<int>v3(10, 100);
printVector(v3);
cout << endl;
vector<int> v4(v3);
printVector(v4);
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
互换容器
#include<iostream>
#include<vector>
using namespace std;
/*
* 实现两个容器内元素进行互换
* swap(vec); 将vec与本身的元素互换
*/
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;
vector<int>v2;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
v2.push_back(9 - i);
}
printVector(v1);
printVector(v2);
v1.swap(v2);
cout << "交换之后为:" << endl;
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 << endl;
cout << "v的容量为:" << v.capacity() << endl;
cout << "v的大小为:" << v.size() << endl;
vector<int>(v).swap(v);
//vector<int>(v) 匿名对象 当前行执行完 系统自动回收
cout << endl;
cout << "v的容量为:" << v.capacity() << endl;
cout << "v的大小为:" << v.size() << endl;
}
int main()
{
/*test01();*/
test02();
system("pause");
return 0;
}
容量和大小
#include<iostream>
#include<vector>
using namespace std;
//对vector容器的容量和大小操作
/*
* empty(); 判断容器是否为空
* capacity(); 容器的容量
* size(); 返回容器中元素的个数
* resize(int num); 重新指定容器的长度为num,若容器变长,则以默认值填充新位置
* 如果容器变短,则末尾超出容器长度的元素被删除
* resize(int num, elem); 重新指定容器的长度为num,若容器变长,则以elem值填充新位置
* 如果容器变短,则末尾超出容器长度的元素被删除
*/
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())
{
cout << "v1为空" << endl;
}
else
{
cout << "v1为空" << endl;
cout << "v1的容量为:" << v1.capacity() << endl;
cout << "v1的大小为:" << v1.size() << endl;
}
//重新指定大小
v1.resize(15,100);//多余的默认为 0
printVector(v1);
v1.resize(5);
printVector(v1);
}
int main()
{
test01();
system("pause");
return 0;
}
数据存取
#include<iostream>
#include<vector>
using namespace std;
//对vector中的数据进行存取操作
/*
* at(int idx); 返回索引idx所指的数据
* operator[]; 返回索引idx所指的数据
* front(); 返回容器中第一个数据元素
* back(); 返回容器中最后一个数据元素
*/
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);
for (int i = 0; i < v1.size(); i++)
{
cout << v1[i] << " ";
}
cout << endl;
for (int i = 0; i < v1.size(); i++)
{
cout << v1.at(i) << " ";
}
cout << endl;
cout << "第一个元素为:" << v1.front()<<endl;
cout << "最后一个元素为:" << v1.back() << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
预留空间
#include<iostream>
#include<vector>
using namespace std;
/*
* 减少vector在动态扩展容量时的扩展次数
* resever(int len); 容器预留len个元素长度,预留位置不初始化,元素不可访问
*/
void test01()
{
vector<int>v;
int num = 0;
int* p = NULL;
v.reserve(100000);
for (int i = 0; i < 100000; i++)
{
v.push_back(i);
if (p != &v[0])
{
p = &v[0];
num++;
}
}
cout << "num = " << num << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
string容器
构造函数
/*string是c++风格的字符串,string本质上是一个类
string和char*的区别
char*是一个指针
string是一个类,类内部封装了char*管理这个字符串,是一个char*型的容器
string;类内部封装了很多成员方法
例如 查找find 拷贝copy 删除delete 替换replace 插入insert
string管理char*所分配的内存 不用担心复制越界和取值越界等 由类内部进行负责*/
/*构造函数原型
string(); 创建一个空的字符串
string(const char* s); 使用字符串s初始化
string(const string&str); 使用一个string对象初始化另一个string对象
string(int n,char c); 使用n个字符c初始化*/
#include<string>
#include<iostream>
using namespace std;
void test01()
{
string s1;
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赋值操作
#include<iostream>
#include<string>
using namespace std;
/*
* string& operator=(const char* s); char*类型字符串 赋值给当前字符串
* string& operator=(const string &s); 把字符串s赋值给当前字符串
* string& operator=(char c); 字符赋值给当前字符串
* string& assign(const char *s); 把字符串s赋值给当前字符串
* string& assign(const char *s, int n); 把字符串s的前n个字符赋值给当前的字符串
* string& assign(const string &s); 把字符串s赋值给当前字符串
* string& assign(int a, char c); 用n个字符c赋值给当前字符串
*/
void test01()
{
string str1;
str1 = "hello world";
cout << str1 << endl;
string str2;
str2.assign("hello c++");
cout << str2 << endl;
string str3;
str3.assign("hello c++", 5);
cout << str3 << endl;
string str4;
str4.assign(str3);
cout << str4 << endl;
string str5;
str5.assign(10, 'a');
cout << str5 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
字串获取
#include<iostream>
#include<string>
using namespace std;
/*
* string substr(int pos = 0,int n = npos) const; 返回由pos开始的n个字符组成的字符串
*/
void test01()
{
string str = "abcdefg";
string str1 = str.substr(0, 3);
cout << str1 << endl;
}
void test02()
{
string email = "1905510756@qq.com";
int a = email.find("@");
string usrName = email.substr(0, a);
cout << usrName << endl;;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
字符串比较
#include<iostream>
#include<string>
/*
* = 返回 0
* > 返回 1
* < 返回 -1
*
* int compare(const string &s) const;
* int compace(const char *s) const;
*/
using namespace std;
void test01()
{
string str1 = "hello";
string str2 = "hello";
if (str1.compare(str2) == 0)
{
cout << "str1 = str2" << endl;
}
else if (str1.compare(str2) > 0)
{
cout << "str1 大于 str2" << endl;
}
else
{
cout << "str1小于 str2" << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
字符串插入和删除
#include<iostream>
#include<string>
using namespace std;
/*
* string& insert(int pos, const char* s);//插入字符串
* string& insert(int pos, const string& s);//插入字符串
* string& insert(int pos, int n, char c);//在指定位置插入n个字符c
* string& erase(int pos, int n = pos);//删除从pos开始的n个字符
*/
void test01()
{
string str = "hello";
str.insert(1, "111");
cout << str << endl;
str.erase(1, 3);
cout << str << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
字符串查找和替换
#include<iostream>
#include<string>
using namespace std;
/*
* int find(const string& str ,int pos = 0) const; 查找str第一次出现位置,从pos开始查找
* int find(const char* s, int pos = 0) const; 查找s第一次出现位置,从pos开始查找
* int find(const char* s,int pos ,int n) const; 从pos位置查找s的前n个字符第一次位置
* int find(const char c, int pos = 0) const; 查找字符c第一次出现位置
* int rfind(const string& str, int pos = npos) const;查找str最后一次位置,从pos开始查找
* int rfind(const char* s, int pos = npos) const; 查找s最后一次出现位置,从pos开始查找
* int rfind(const char* s, int pos ,int n) const; 从pos查找s的前n个字符最后一次位置
* int rfind(char c,int pos = 0) const; 查找字符c最后一次出现位置
* string& replace(int pos, int n,const string& str); 替换从pos开始n个字符为字符串str
* string& replace(int pos, int n,const char* s); 替换从pos开始的n个字符为字符串s
*/
//查找
void test01()
{
string str1 = "abcdefg";
int pos=str1.find("de");
if (pos == -1)
{
cout << "未找到字符串" << endl;
}
else
cout << "pos = " << pos << endl;
//rfind
//rfind 和 find 的区别 ,rfind是从右往左查 find相反
pos = str1.rfind("de");
cout << "pos = " << pos << endl;
}
//替换
void test02()
{
string str1 = "abcdefg";
str1.replace(1, 3,"1111");
cout << str1;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
字符串拼接
#include<iostream>
#include<string>
using namespace std;
/*
* string& opterator+=(const char* str); 重载+=运算符
* string& opterator+=(const char c); 重载+=运算符
* string& opterator+=(const string& str);重载+=运算符
* string& append(const char *s); 把字符s连接到当前字符结尾
* string& append(const char *s, int n); 把字符串s的前n个字符连接到当前字符串结尾
* string& append(const string &s); 同opterator+=(const string& str)
* string& append(const string &s, int pos ,int n); 字符串s中从pos开始的n给字符连接到字符串结尾
*/
void test01()
{
string str1 = "我";
str1 += "爱玩游戏";
cout << str1 << endl;
str1 += ';';
cout << str1 << endl;
string str2 = "LOL DNF";
str1 += str2;
cout << str1 << endl;
string str3 = "I";
str3.append(" love ");
cout << str3 << endl;
str3.append("game abcde", 4);
cout << str3 << endl;
str3.append(str2);
cout << str3 << endl;
str3.append(str2, 0, 3);
cout << str3 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
字符存取
#include<iostream>
#include<string>
using namespace std;
//string中单个字符存取方式有两种
/*
* char& operator[](int n); 通过[]方式取字符
* char& at(int n); 通过at方法获取字符
*/
void test01()
{
string str1 = "hello";
//cout << "str1 = " << str1 << endl;
for (int i = 0; i < str1.size(); i++)
{
cout << str1[i]<<" ";
}
cout << endl;
for (int i = 0; i < str1.size(); i++)
{
cout << str1.at(i)<< " ";
}
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
map-muiltimap容器
构造和赋值
#include<iostream>
using namespace std;
#include<map>
/*
* map中所有元素都是pair
* pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值)
* 所有元素都会根据元素的键值自动排序
*/
// map/multimap 属于关联式容器,底层结构是用二叉树实现
/*
* 优点:
* 可以根据key值快速找到value值
*
* map/multimap 区别
* map不允许容器中有重复key值元素
* multimap允许容器中有重复key值元素
*/
/*
* map构造和赋值
* map<T1, T2>mp;
* map(const map &mp);
*
* map& operator=(const map &mp);
*/
void printMap(map<int, int>m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << it->first << " " << it->second<<endl;
}
}
void test01()
{
map<int, int>m;
/*m.insert(1, 1);*/
m.insert(pair<int, int>(2, 10));
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(3, 10));
m.insert(pair<int, int>(4, 10));
printMap(m);
cout << "---------------" << endl;
map<int, int>m2(m);
printMap(m2);
cout << "---------------" << endl;
map<int, int>m3;
m3 = m2;
printMap(m3);
}
int main()
{
test01();
system("pause");
return 0;
}
插入和删除
#include<iostream>
using namespace std;
#include<map>
/*
* insert(elem); 在容器中插入元素
* clear(); 清除所有元素
* erase(pos); 删除pos迭代器所指的元素,返回下一个元素的迭代器
* erase(beg, end); 删除区间[beg, end)的所有元素,返回下一个元素的迭代器
* erase(key); 删除容器中值为key的值
*/
void printMap(map<int, int>& m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << it->first << " " << it->second << endl;
}
cout << endl;
}
void test01()
{
map<int, int>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; //[]不建议插入,可以利用key访问value
printMap(m);
m.erase(m.begin());
printMap(m);
m.erase(3);
printMap(m);
m.erase(m.begin(), m.end());
printMap(m);
}
int main()
{
test01();
system("pause");
return 0;
}
查找和统计
#include<iostream>
using namespace std;
#include<map>
/*
* find(key); 查找key是否存在,返回该键的元素的迭代器,若不存在,返回set.end();
* count(key); 统计key的个数
*/
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));
map<int, int>::iterator pos = m.find(3);
if (pos != m.end())
{
cout << "查到元素 key = " << pos->first << " value = " << pos->second << endl;
}
else
{
cout << "未找到!" << endl;
}
int num = m.count(3); //map不允许插入重复元素,count统计而言 结果要么是0 要么是1
//multimap 的count结果可能大于1
cout << "num = " << num << endl;
//统计
}
int main()
{
test01();
system("pause");
return 0;
}
大小和交换
#include<iostream>
using namespace std;
#include<map>
/*
* size();
* empty();
* swap(st);
*/
void printMap(map<int,int>& m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << it->first << " " << it->second << endl;
}
cout << endl << endl;
}
void test01()
{
map<int, int>m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 10));
m.insert(pair<int, int>(3, 10));
m.insert(pair<int, int>(4, 10));
printMap(m);
if (m.empty())
{
cout << "m为空!" << endl;
}
else
{
cout << "m不为空!" << endl;
cout << "m的大小为:" << m.size()<<endl;
}
map<int, int>m2;
m2.insert(pair<int, int>(5, 10));
m2.insert(pair<int, int>(6, 10));
m2.insert(pair<int, int>(7, 10));
m2.insert(pair<int, int>(8, 10));
printMap(m2);
m2.swap(m);
printMap(m);
printMap(m2);
}
int main()
{
test01();
system("pause");
return 0;
}
排序
#include<iostream>
using namespace std;
#include<map>
/*
* 利用仿函数,可以改变排序规则
*/
class MyCompare
{
public:
bool operator()(int v1, int v2)const
{
return v1 > v2;
}
};
void printMap(map<int, int>& m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << it->first << " " << it->second << endl;
}
}
void test01()
{
map<int, int>m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 10));
m.insert(pair<int, int>(3, 10));
m.insert(pair<int, int>(4, 10));
printMap(m);
map<int, int,MyCompare>m1;
m1.insert(pair<int, int>(1, 10));
m1.insert(pair<int, int>(2, 10));
m1.insert(pair<int, int>(3, 10));
m1.insert(pair<int, int>(4, 10));
cout << endl;
for (map<int, int, MyCompare>::iterator it = m1.begin(); it != m1.end(); it++)
{
cout << it->first << " " << it->second << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
set容器
基本概念
#include<iostream>
using namespace std;
#include<set>
/*
* 集合容器
* 所有元素都会在插入时自动排序
* set/multiset属于关联式容器,底层结构用二叉树实现
* set和multiset区别
* set不允许容器中有重复元素
* multiset允许有重复元素
*/
/*
* set构造和赋值
* set<T> st; 默认构造函数
* set(const set& st); 拷贝构造函数
*
* set& operator=(const set &st); 重载等号运算符
*/
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;
//插入数据只有insert方式
s1.insert(10);
s1.insert(0);
s1.insert(40);
s1.insert(20);
s1.insert(30);
s1.insert(10);
printSet(s1);
//set不允许插入重复值
set<int>s2(s1);
set<int>s3 = s1;
printSet(s2);
printSet(s3);
}
int main()
{
test01();
system("pause");
return 0;
}
插入和删除
#include<iostream>
using namespace std;
#include<set>
/*
* insert(elem);
* clear();
* erase(pos); 删除pos迭代器所指元素,返回下一个元素的迭代器
* erase(beg, end);删除区间[beg, end)的所有元素,返回下一个元素的迭代器
* erase(elem); 删除容器中值为elem的元素
*/
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(30);
s1.insert(15);
printSet(s1);
s1.erase(s1.begin());
printSet(s1);
s1.erase(30);
printSet(s1);
s1.erase(s1.begin(), s1.end());
printSet(s1);
}
int main()
{
test01();
system("pause");
return 0;
}
pair对组创建
#include<iostream>
using namespace std;
#include<set>
#include<string>
/*
* 成对出现的数据,利用队组可以返回两个数据
* pair<type, type> p (value1, value2);
* pair<type, type> p = make_pair(value1, value2);
*/
void test01()
{
pair<string, int>p(string("Tom"), 20);
cout << "姓名:" << p.first << " 年龄:" << p.second << endl;
pair<string, int>p2 = make_pair("niuniu", 20);
cout << "姓名:" << p2.first << " 年龄:" << p2.second << endl;
}
int main()
{
system("pause");
return 0;
}
查找和统计
#include<iostream>
using namespace std;
#include<set>
/*
* find(key); 查找key是否存在,返回该键的元素的迭代器,若不存在,返回set.end();
* count(key); 统计key的个数
*/
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(30);
s1.insert(15);
printSet(s1);
set<int>::iterator pos = s1.find(30);
if (pos != s1.end())
{
cout << "找到元素:" << *pos << endl;
}
else
{
cout << "为找到元素:" << *pos << endl;
}
cout << "共有" << s1.count(30) << "个30" << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
大小和交换
#include<iostream>
using namespace std;
#include<set>
/*
* 统计set容器大小以及交换set容器
* size(); 返回容器中元素数目
* empty(); 判断容器是否为空
* swap(st); 交换两个集合容器
*/
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(30);
s1.insert(15);
printSet(s1);
if (s1.empty())
{
cout << "s1为空!" << endl;
}
else
{
cout << "s1不为空" << endl;
}
set<int>s2;
for (int i = 0; i < 5; i++)
{
s2.insert(i);
}
printSet(s2);
s2.swap(s1);
printSet(s1);
printSet(s2);
}
int main()
{
test01();
system("pause");
return 0;
}
set容器排序
#include<iostream>
using namespace std;
#include<set>
/*
* set容器默认排序规则为从小到大
* 掌握如何改变排序规则
* 利用仿函数,可以改变排序规则
*/
/*
严重性 代码 说明 项目 文件 行 禁止显示状态
错误 C3848 具有类型“const MyCompare”的表达式会丢失一些 const-volatile 限定符以调用“bool MyCompare::operator ()(int,int)” set容器 D:\vs\VC\Tools\MSVC\14.37.32822\include\xutility 1352
*///在仿函数后面加上const
class MyCompare
{
public:
bool operator()(int v1,int v2) const
{
return v1 > v2;
}
};
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(30);
s1.insert(15);
printSet(s1);
set<int, MyCompare>s2;
s2.insert(10);
s2.insert(20);
s2.insert(30);
s2.insert(15);
for (set<int, MyCompare>::iterator it = s2.begin(); it != s2.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
自定义数据类型指定排序该规则
#include<iostream>
using namespace std;
#include<set>
#include<string>
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) const
{
return p1.m_Age > p2.m_Age;
}
};
void test01()
{
//自定义数据类型,都会指定排序规则
set<Person,comparePerson>s;
Person p1("刘备", 24);
Person p2("关羽", 28);
Person p3("张飞", 25);
Person p4("赵云", 21);
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<< endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
set和multiset区别
#include<iostream>
#include<set>
using namespace std;
/*
* set不可以插入重复数据
* multuiset可以
* set插入数据的同时会返回插入结果 表示插入是否成功
* multiset不会检测数据 因此可以重复插入数据
*/
void printSet(set<int>& s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
set<int> s;
s.insert(10);
//printSet(s1);
pair<set<int>::iterator, bool>ret = s.insert(10);
if (ret.second)
{
cout << "第一次插入成功" << endl;
}
else
{
cout << "第一次插入失败" << endl;
}
multiset<int>s2;
s2.insert(10);
}
int main()
{
test01();
system("pause");
return 0;
}
queue容器
基本概念
#include<iostream>
#include<queue>
using namespace std;
/*
* queue 是一种先进先出的数据结构,他有两个出口
* 队列容器允许从一端新增元素,从另一端移除元素
* 队列只有队头和队尾才可以被外界使用,因此队列不允许有遍历行为
* 队列中进数据称为 入队
* 出 出队
*/
int main()
{
system("pause");
return 0;
}
常用接口
#include<iostream>
#include<queue>
#include<string>
using namespace std;
/*
* 构造函数:
* queue<T>que; queue采用模板类实现,queue对象的默认构造形式
* queue(const queue &que); 拷贝构造函数
*
* 赋值操作:
* queue& operator=(const queue &que); 重载等号操作符
*
* 数据存取:
* push(elem); 往队尾添加元素
* pop(); 从队头移除第一个元素
* back(); 返回最后一个元素
* front(); 返回第一个元素
*
* 大小操作:
* empty(); 判断堆栈是否为空
* size(); 返回栈的大小
*/
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("HRY", 1);
Person p2("HRY1", 2);
Person p3("HRY2", 3);
Person p4("HRY3", 4);
q.push(p1);
q.push(p2);
q.push(p3);
q.push(p4);
while (!q.empty())
{
cout << "队头元素----姓名:" << q.front().m_Name << " 年龄:" << q.front().m_Age<< endl;
cout << "队尾元素----姓名:" << q.back().m_Name << " 年龄:" << q.back().m_Age << endl;
q.pop();
}
cout << "队列大小为:" << q.size();
}
int main()
{
test01();
system("pause");
return 0;
}
stack容器
基本概念
#include<iostream>
using namespace std;
/*
* stack是一种先进后出的数据结构,他只有一个出口
* 栈中只有顶端元素才可以被外界使用,因此栈不允许有遍历行为
* 入栈 push
* 出栈 pop
*/
/*
* 构造函数:
* stack<T>stk; stack采用模板类实现,stack对象的默认构造形式
* stack(const stack &stk); 拷贝构造函数
*
* 赋值操作:
* stack& operator=(const stack &stck); 重载等号操作符
*
* 数据存取:
* push(elem); 向栈顶添加元素
* pop(); 从栈顶移除第一个元素
* top(); 返回栈顶元素
*
* 大小操作:
* empty(); 判断堆栈是否为空
* size(); 返回栈的大小
*/
int main()
{
system("pause");
return 0;
}
常用接口
#include<iostream>
#include<stack>
using namespace std;
/*
* 构造函数:
* stack<T>stk; stack采用模板类实现,stack对象的默认构造形式
* stack(const stack &stk); 拷贝构造函数
*
* 赋值操作:
* stack& operator=(const stack &stck); 重载等号操作符
*
* 数据存取:
* push(elem); 向栈顶添加元素
* pop(); 从栈顶移除第一个元素
* top(); 返回栈顶元素
*
* 大小操作:
* empty(); 判断堆栈是否为空
* size(); 返回栈的大小
*/
void test01()
{
stack<int>s;
s.push(10);
s.push(20);
s.push(30);
s.push(40);
while (!s.empty())
{
cout << "栈顶元素为:" << s.top() << endl;
s.pop();
}
cout << "栈的大小为:" << s.size() << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
deque容器
构造函数
#include<iostream>
#include<deque>
using namespace std;
//双端数组 可以对头端进行插入删除操作
/*
* deque与vector的区别
* vector对于头部的插入效率低,数据量越大 效率越低
* deque相对而言,对头部的插入删除速度会比vector快
* vector访问元素时的速度会比deque快,这和两者的内部实现有关
* deque内部工作原理:
* deque内部有个中控器,维护每段缓冲区的内容,缓冲区存放真实数据
* 中控器维护的是每个缓冲区的地址,使得使用deque时像一片连续的内存空间
* deque容器的迭代器是支持随机访问的
*/
/*
* deque<T>deqT; 默认构造形式
* deque(beg, end); 构造函数将[beg, end)区间中的元素拷贝给本身
* deque(n, elem); 给构造函数将n个elem拷贝给本身
* deque(const deque &deq); 拷贝构造函数
*/
void printDeque(const deque<int>&d)//加上const后 下方的iterator要变为const_iterator
{
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, 100);
printDeque(d3);
deque<int>d4(d3);
printDeque(d3);
}
int main()
{
test01();
system("pause");
return 0;
}
赋值操作
#include<iostream>
#include<deque>
using namespace std;
/*
* deque& operator=(const deque &deq); 重载等号运算符
* assign(deg, end); 将[beg, end)区间中的数据拷贝赋值给本身
* assign(n, elem); 将n个elem拷贝赋值给本身
*/
void printDeque(const 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 = d1;
printDeque(d2);
deque<int>d3;
d3.assign(d1.begin(), d1.end());
printDeque(d3);
deque<int>d4;
d4.assign(10, 100);
printDeque(d4);
}
int main()
{
test01();
system("pause");
return 0;
}
插入和删除
#include<iostream>
#include<deque>
using namespace std;
/*
* push_back(elem); 尾部添加
* push_front(elen); 首部添加
* pop_back(); 删除最后
* pop_front(); 删除第一个
*/
/*
* insert(pos,elem); pos位置插入elem元素的拷贝,返回新数据的位置
* insert(pos,n,elem); pos位置插入n个elem数据,无返回值
* insert(pos,beg,end); pos位置插入[beg, end)区间的数据 ,无返回值
* clear(); 清空容器内的数据
* earse(beg,end); 删除[beg,end)区间的数据,返回下一个数据的位置
* erase(pos); 删除pos位置的数据,返回下一个数据的位置
*/
void printDeque(const 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_front(20);
d1.push_front(10);
printDeque(d1);
d1.pop_back();
d1.pop_front();
printDeque(d1);
}
void test02()
{
deque<int>d1;
d1.push_back(10);
d1.push_back(20);
d1.push_front(200);
d1.push_front(100);
printDeque(d1);
d1.insert(d1.begin(), 1000);
d1.insert(d1.begin(),2, 90);
printDeque(d1);
deque<int>d2;
d2.push_back(1);
d2.push_back(2);
d2.push_back(3);
d1.insert(d1.begin(), d2.begin(), d2.end());
printDeque(d1);
deque<int>::iterator it = d1.begin();
//it++;
d1.erase(it);
printDeque(d1);
d1.erase(d1.begin(), d1.end());
printDeque(d1);
}
int main()
{
/*test01();*/
test02();
system("pause");
return 0;
}
数据存取
#include<iostream>
#include<deque>
using namespace std;
/*
* at(int idx); 返回索引idx所指的数据
* operator[]; 返回索引idx所指的数据
* front(); 返回容器中第一个数据元素
* back(); 返回容器中最后一个数据元素
*/
void printDeque(const 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_front(100);
d1.push_front(200);
d1.push_front(300);
for (int i = 0; i < d1.size(); i++)
{
cout << d1[i] << " ";
}
cout << endl;
for (int i = 0; i < d1.size(); i++)
{
cout << d1.at(i) << " ";
}
cout << endl;
cout << "d1的第一个元素:" << d1.front() << endl;
cout << "d1的最后一个元素:" << d1.back() << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
大小操作
#include<iostream>
#include<deque>
using namespace std;
/*
* deque.empty(); 判断容器是否为空
* deque.size(); 返回容器中元素的个数
* deque.resize(num); 重新指定容器的长度为num,若容器变长,则以默认值填充新位置
* 如果容器变短,则末尾超出容器长度的元素被删除
* deque.resize(num, elem); 重新指定容器的长度为num,若容器变长,则以elem值填充新位置
* 如果容器变短,则末尾超出容器长度的元素被删除
*/
void printDeque(const 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;
cout << "d1的大小为:" << d1.size() << endl;
//deque中没有容量(capacity)的概念
}
//重新指定大小
d1.resize(11);
printDeque(d1);
d1.resize(15,1);
printDeque(d1);
}
int main()
{
test01();
system("pause");
return 0;
}
排序操作
#include<iostream>
#include<deque>
#include<algorithm>//标准算法头文件
using namespace std;
/*
* 利用算法实现对deque容器进行排序
* sort(iterator beg, iterator end); 对beg和end区间内元素进行排序
*/
void printDeque(const deque<int>& d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
deque<int>d;
d.push_back(10);
d.push_back(20);
d.push_back(30);
d.push_front(100);
d.push_front(200);
d.push_front(300);
printDeque(d);
sort(d.begin(), d.end());
printDeque(d);
/*
* 排序 默认排序该规则为从小到大 升序
* 对于支持随机访问的迭代器的容器 都可以利用sort算法直接对其进行排序
* vector容器也可以利用 sort进行排序
*/
}
int main()
{
test01();
system("pause");
return 0;
}
list容器
基本概念
#include<iostream>
using namespace std;
#include<list>
/*
* 功能:将数据进行链式存储
* 链表是一种物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链接实现的
* 链表的组成:链表由一系列节点组成
* 节点的组成:一个是存储数据元素的数据域,另一个是存储下一个节点地址的指针域
* STL中的链表是一个双向循环链表
*/
int main()
{
system("pause");
return 0;
}
构造函数
#include<iostream>
using namespace std;
#include<list>
/*
* 创建list容器
* list<T>lst; list采用模板类实现,对象的默认构造形式
* list(beg, end); 构造函数[beg,end)区间中的元素拷贝给本身
* list(n,elem); 构造函数将n个elem拷贝给本身
* list(const list &lst); 拷贝构造函数
*/
void printList(const 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);
list<int>l4(10,100);
printList(l4);
}
int main()
{
test01();
system("pause");
return 0;
}
赋值和交换
#include<iostream>
#include<list>
using namespace std;
/*
* 给list容器进行赋值,以及交换list容器
* assign(beg, end); 将[beg, end)区间中的数据拷贝赋值给本身
* assign(n, elem); 将n个elem拷贝赋值给本身
* list& operator=(const list &lst); 重载等号操作符
* swap(lst); 将lst与本身的元素互换
*/
void printList(list<int>& l)
{
for (list<int>::iterator it = l.begin(); it != l.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
template<class T>
void test01()
{
list<T>l1;
l1.push_back(10);
l1.push_back(20);
l1.push_back(30);
l1.push_back(40);
list<int>l2;
l2 = l1;
printList(l2);
list<int>l3;
l3.assign(l2.begin(), l2.end());
printList(l3);
list<int>l4;
l4.assign(10, 100);
printList(l4);
}
void test02()
{
list<int>l1;
l1.push_back(10);
l1.push_back(20);
l1.push_back(30);
l1.push_back(40);
list<int>l4;
l4.assign(10, 100);
l1.swap(l4);
printList(l1);
printList(l4);
}
int main()
{
test01<int>();
test02();
system("pause");
return 0;
}
插入和删除
#include<iostream>
using namespace std;
#include<list>
//对list容器进行数据的插入和删除
/*
* push_back(elem); 在容器尾部加入一个元素
* pop_back(); 删除容器中最后一个元素
* push_front(elem); 在容器开头加入一个元素
* pop_front(); 从容器开头移除第一个元素
* insert(pos,elem); 在pos位置插elem元素的拷贝,返回新数据的位置
* insert(pos,n,elem); 在pos位置插入n个elem数据,无返回值
* insert(pos,beg,end); 在pos位置插入[beg, end)区间的数据,无返回值
* clear(); 移除容器中所有元素
* erase(beg,end); 删除区间[beg, end)区间的数据,返回下一个数据的位置
* erase(pos); 删除pos位置的数据,返回下一个数据的位置
* remove(elem); 删除容器中所有与elem值匹配的元素
*/
void printList(const list<int>& l)
{
for (list<int>::const_iterator it = l.begin(); it != l.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
list<int>l;
l.push_back(10);
l.push_back(20);
l.push_back(30);
l.push_front(100);
l.push_front(200);
l.push_front(300);
printList(l);
l.pop_back();
l.pop_front();
printList(l);
list<int>::iterator it = l.begin();
l.insert(++it, 1000);
printList(l);
it = l.begin();
l.erase(it);
printList(l);
l.remove(1000);
printList(l);
}
int main()
{
test01();
system("pause");
return 0;
}
数据存取
#include<iostream>
using namespace std;
#include<list>
/*
* front();返回第一个元素
* back();返回最后一个元素
*/
void printList(const 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);
//l1[0] 该访问方式错误
//l1.at(0) 该访问方式也错误
//list本质是链表,不是用连续线性空间存储数据,迭代器不支持随机访问
cout << "第一个元素:" << l1.front() << endl;
cout << "最后一元素:" << l1.back() << endl;
list<int>::iterator it = l1.begin();
//it = it + 1; 错误
it++;
it--;
}
int main()
{
test01();
system("pause");
return 0;
}
大小操作
#include<iostream>
using namespace std;
#include<list>
/*
* size();
* empty();
* resize();
* resize(num, elem);
*/
void printList(const 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);
if (l1.empty())
{
cout << "l1为空!" << endl;
}
else
{
cout << "l1不为空!" << endl;
cout << "l1的元素个数为:" << l1.size() << endl;
}
l1.resize(10);
printList(l1);
}
int main()
{
test01();
system("pause");
return 0;
}
反转和排序
#include<iostream>
using namespace std;
#include<list>
/*
* 将容器中的元素反转,以及将容器中的数据进行排序
* reverse(); 反转链表
* sort(); 链表排序
*/
bool MyCompare(int a, int b)
{
return a > b;
}
void printList(const 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(20);
l1.push_back(310);
l1.push_back(10);
l1.push_back(999);
printList(l1);
l1.reverse();
printList(l1);
l1.sort();
printList(l1);
l1.sort(MyCompare);
printList(l1);
}
int main()
{
test01();
system("pause");
return 0;
}
排序案例
#include<iostream>
using namespace std;
#include<string>
#include<list>
/*
* 案例描述:将person自定义数据类型进行排序,person中属性有姓名,年龄,身高
* 排序规则:按照年龄进行升序,如果年龄相同则按照身高进行升序
*/
class Person
{
public:
Person(string name, int age, int height)
{
this->m_Age = age;
this->m_Height = height;
this->m_Name = name;
}
string m_Name;
int m_Age;
int m_Height;
};
bool comparePerson(Person& p1, Person p2)
{
return p1.m_Age < p2.m_Age;
}
void test01()
{
list<Person>l;
Person p1("刘备", 35, 175);
Person p2("曹操", 45, 180);
Person p3("孙权", 40, 170);
Person p4("赵云", 25, 190);
Person p5("张飞", 35, 160);
Person p6("关羽", 35, 200);
l.push_back(p1);
l.push_back(p2);
l.push_back(p3);
l.push_back(p4);
l.push_back(p5);
l.push_back(p6);
for (list<Person>::iterator it = l.begin(); it != l.end(); it++)
{
cout << "姓名: " << (*it).m_Name
<< " 年龄: " << it->m_Age << " 身高:" << it->m_Height<<endl;
}
cout << endl << "-------------------------" << endl << "排序后:" << endl;
l.sort(comparePerson);
for (list<Person>::iterator it = l.begin(); it != l.end(); it++)
{
cout << "姓名: " << (*it).m_Name
<< " 年龄: " << it->m_Age << " 身高:" << it->m_Height << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
STL案例
案例1-评委打分
#include<iostream>
#include<string>
#include<deque>
#include<vector>
#include<algorithm>
#include<ctime>
using namespace std;
/*
* 有无名选手:选手ABCDE,10个评委分别对每一个选手打分,去除最高分,去除评委中最低分,取平均
* 1.创建五名选手,放到vector中
* 2.遍历vector容器,取出每一个选手,执行for循环,可以把10个评分存到deque容器中
* 3.sort算法对deque容器中分数进行排序,去除最高分和最低分
* 4.deque容器遍历一遍,累加总分
* 5.获取平均分
*/
class Person
{
public:
Person(string name, int scorce)
{
this->m_Name = name;
this->m_Scorce = scorce;
}
string m_Name;//姓名
int m_Scorce;//平均分
};
void creatPerson(vector<Person>& v)
{
string nameSeed = "ABCDE";
for (int i = 0; i < 5; i++)
{
string name = "选手-";
name += nameSeed[i];
int scorce = 0;
Person p(name, scorce);
//将创建的person对象放置到容器中
v.push_back(p);
}
}
void setScorce(vector<Person>& v)
{
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
{
deque<int>d;
for (int i = 0; i < 10; i++)
{
int scorce = rand() % 41 + 60;
d.push_back(scorce);
}
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_Scorce = avg;
}
}
void showScorce(vector<Person>& p)
{
for (vector<Person>::iterator it = p.begin(); it != p.end(); it++)
{
cout << "姓名:" << it->m_Name << " 平均分:" << it->m_Scorce << endl;
}
}
int main()
{
//随机数种子
srand((unsigned int)time(NULL));
//1.创建五名选手
vector<Person>v;
creatPerson(v);
/*for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
{
cout << "name:" << (*it).m_Name << " sorce:" << (*it).m_Scorce << endl;
}*/
//2.给五名选手打分
setScorce(v);
showScorce(v);
//3.显示最后得分
system("pause");
return 0;
}
案例2-员工分组
#include<iostream>
using namespace std;
#include<set>
#include<vector>
#include<map>
#include<ctime>
#include<string>
/*
* 公司今天招聘了10个员工(ABCDEFGHIJ),10名员工进入公司之后,需要指派员工在那个部门工作
* 员工信息有:姓名 工资组成;部门分为:策划、美术、研发
* 随机给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:
string m_Name;
int m_Salary;
};
void createWorker(vector<Worker>&v)
{
string nameSeed = "ABCDEFGHIJ";
for (int i = 0; i < 10; i++)
{
Worker worker;
worker.m_Name = "员工";
worker.m_Name += nameSeed[i];
worker.m_Salary = rand() % 10000 + 10000;
v.push_back(worker);
}
}
void setGroup(vector<Worker>& v, multimap<int, Worker>& m)
{
for (vector<Worker>::iterator it = v.begin(); it != v.end(); it++)
{
int deptId = rand() % 3;
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_Salary << endl;
}
cout <<"---------------------" <<endl<< "美术部门:" << 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_Salary << endl;
}
cout << "---------------------"<<endl << "研发部门:" << 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_Salary << endl;
}
}
int main()
{
//随机数种子
srand((unsigned int)time(NULL));
vector<Worker>vWorker;
createWorker(vWorker);
/*for (vector<Worker>::iterator it = vWorker.begin(); it != vWorker.end(); it++)
{
cout << "name: " << it->m_Name << " salary:" << it->m_Salary << endl;
}*/
//员工分组
multimap<int, Worker>mWorker;
setGroup(vWorker, mWorker);
showWorkerByGroup(mWorker);
system("pause");
return 0;
}