3 STL-常用的容器
时间:2023-10-31 00:14 作者:wen 分类: C++
[toc]
常用的容器
string容器
string基本概念
本质:
- string是C++风格的字符串,而string本质上是一个类
string和**char *** 区别
- char * 是一个指针
- string是一个类,类内部封装了char *,管理这个字符串,是一个char * 型的容器
特点:
string类内部封装了很多成员方法
例如:查找find,拷贝copy,删除delete,替换replace,插入insert
string管理char * 所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责
string构造函数
构造函数原型:
string();
//创建一个空的字符串,例如:string str;string(const char* s);
//使字符串s初始化string(const string& str);
//使一个string对象初始化另一个string对象string(int n, char c);
//使用n个字符c初始化
示例:
#include <iostream>
#include <string>
using namespace std;
//string构造函数
void testA()
{
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()
{
testA();
return 0;
}
string的赋值操作
功能描述:
- 给string字符串进行赋值
赋值的函数原型:
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 n,char c);
//用n个字符c赋值给当前字符串
示例:
#include <iostream>
#include <string>
using namespace std;
//string的赋值操作
void testA()
{
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;
string str4;
str4.assign("hello C++");
cout << "str4 = " << str4 << endl;
string str5;
str5.assign("hello C++", 5);
cout << "str5 = " << str5 << endl;
string str6;
str6.assign(str5);
cout << "str6 = " << str6 << endl;
string str7;
str7.assign(10, 'w');
cout << "str7 = " << str7 << endl;
}
int main()
{
testA();
return 0;
}
string字符串拼接
功能描述:
- 实现在字符串末尾拼接字符串
函数原型:
string& operator+=(const char* str);
//重载+=操作符string& operator+=(const char str);
//重载+=操作符string& operator+=(const string& str);
//重载+=操作符string& append(const char* s);
//把字符串s连接到当前字符串结尾string& append(const char* s, int n);
//把字符串s的前n个字符连接到当前字符串结尾string& append(const string& s);
//同operator+=(const string& str)string& append(const string& s,int pos, int n);
//字符串s中从pos开始的n个字符串连接到字符串结尾
示例:
#include <iostream>
#include <string>
using namespace std;
//string字符串拼接
void testA()
{
string str1 = "我";
str1 += "爱玩游戏";
cout << "str1 = " << str1 << endl;
str1 += ':';
cout << "str1 = " << str1 << endl;
string str2 = "LOL DNF";
str1 += str2;
cout << "str1 = " << str1 << endl;
string str3 = "I";
str3.append(" LOVE ");
cout << "str3 = " << str3 << endl;
str3.append("game abcde", 5);
cout << "str3 = " << str3 << endl;
str3.append(str2);
cout << "str3 = " << str3 << endl;
str3.append(str2, 3, 4);
cout << "str3 = " << str3 << endl;
}
int main()
{
testA();
return 0;
}
总结:字符串拼接的重载版本很多,初学阶段记住几种即可
string查找和替换
功能描述:
- 查找:查找指定字符串是否存在
- 替换:在指定的位置替换字符串
函数原型:
int find(const string& str, int pos = 0) const;
//查找str第一次出现的位置,从pos开始查找int find(const char* str, int pos = 0) const;
//查找str第一次出现的位置,从pos开始查找int find(const char* str, int pos = 0, int n) const;
//从pos位置查找str的前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位置查找str的前n个字符最后一次出现位置int rfind(const char c, int pos) const;
//查找字符c最后一次出现位置string& replace(int pos, int n, const string& str);
//替换从pos开始n个字符为字符串strstring& replace(int pos, int n, char* s);
//替换从pos开始n个字符伟字符串s
示例:
#include <iostream>
#include <string>
using namespace std;
//string查找和替换
//1.查找
void testA()
{
string s = "abcdefgde";
// 从左向右查找
int pos = s.find("de");
if (pos == -1)
{
cout << "未找到字符串" << endl;
}
cout << "pos = " << pos << endl;
// 从右向左查找
pos = s.rfind("de");
cout << "pos = " << pos << endl;
}
//2.替换
void testB()
{
string str1 = "abcdefg";
str1.replace(1, 3, "AAAA");
cout << "str1 = " << str1 << endl;
}
int main()
{
testA();
testB();
return 0;
}
总结:
- find查找是从左往右,rfind从右往左
- find找到字符串后返回查找的第一个字符位置,找不到返回-1
- replace在替换时,要指定从哪个位置1起,多少个字符,替换成什么样的字符串
string字符串比较
功能描述:
- 字符串之间的比较
比较方式:
- 字符串比较式按字符的ASCII码进行对比
= 返回 0
> 返回 1
< 返回 -1
函数原型:
int compare(const string& s) const
//与字符串s比较int compare(const char* s) const
//与字符串s比较
示例:
#include <iostream>
#include <string>
using namespace std;
//string比较
void testA()
{
string s1 = "xello";
string s2 = "zello";
int flag = s1.compare(s2);
if (flag == 0)
{
cout << "相等" << endl;
}
else if (flag == 1)
{
cout << "s1 大于 s2" << endl;
}
else {
cout << "s1 小于 s2" << endl;
}
}
int main()
{
testA();
return 0;
}
总结:字符串对比主要是用于比较两个字符串是否相等,判断谁打谁小的意义并不是很大
string字符存取
string中单个字符存取方式有两种
char& operator[](int n)
//通过[]方式取字符char& at(int n)
//通过at方法获取字符
示例:
#include <iostream>
#include <string>
using namespace std;
//string字符存取
void testA()
{
string s = "hello";
cout << s.size() << endl;
//1.通过[]访问单个字节
for (int i = 0; i < s.size(); i++)
{
cout << s[i] << " ";
}
cout << endl;
//2.通过at访问单个字节
for (int i = 0; i < s.size(); i++)
{
cout << s.at(i) << " ";
}
cout << endl;
//修改单个字符
s[2] = 'e';
s.at(3) = 'e';
cout << s << endl;
}
int main()
{
testA();
return 0;
}
string插入和删除
功能描述:
- 对string字符串进行插入和删除字符串操作
函数原型:
string& insert(int pos, const char* s);
//插入字符串string& insert(int pos, const string& s);
//插入字符串string& insert(int pos, int n, char c);
//在指定位置插入n个字符cstring& erase(int pos, int n=npos);
//删除从pos开始的n个字符
示例:
#include <iostream>
#include <string>
using namespace std;
//string插入和删除
void testA()
{
string str = "hello";
//插入
str.insert(1, "xxx");
cout << str << endl;
str.insert(0, 3, 'z');
cout << str << endl;
//删除
str.erase(1, 3);
cout << str << endl;
}
int main()
{
testA();
return 0;
}
总结:插入和删除的起始下标都是从0开始
string子串
功能描述:
- 从字符串中获取想要的子串
函数原型:
string substr(int pos = 0, int n = npos) const;
//返回由pos开始的n个字符组成的字符串
示例:
#include <iostream>
#include <string>
using namespace std;
//string子串
void testA()
{
string str = "hello@sina.com";
string s = str.substr(0, str.find('@'));
cout << s << endl;
}
int main()
{
testA();
return 0;
}
vector容器
vector基本概念
功能:
- vector数据结构和数组非常相似,也称为单端数组
vector与普通数组区别:
- 不同之处在于数组是静态空间,而vector可以动态扩展
动态扩展:
- 并不是在原空间之后续接新空间,而是找更大的内存空间,然后将原数据拷贝新空间,释放原空间
- vector容器的迭代器是支持随机访问的迭代器
vector构造函数
功能描述:
- 创建vector容器
函数原型:
vector<T> v;
//采用模板实现类实现,默认构造函数vector(v.begin(), v.end());
//将v[begin(),end()]区间中的元素拷贝给本身vector(n, elem);
//构造函数将n个elem拷贝给本身vector(const vector &vec);
//拷贝构造函数
示例:
#include <iostream>
#include <string>
using namespace std;
#include <vector>
// vector构造函数
void printVector(vector<int>& v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void testA()
{
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方式构造
vector<int>v3(10, 100);
printVector(v3);
//拷贝构造
vector<int>v4(v3);
printVector(v3);
}
int main()
{
testA();
return 0;
}
vector赋值操作
功能描述:
- 给vector容器进行赋值
函数原型:
vector& operator=(const vector& vec);
//重载等号操作符assign(beg,end);
//将[beg,end]区间中的数据拷贝赋值给本身assign(n,elem);
//将n个elem拷贝赋值给本身
示例:
#include <iostream>
#include <string>
using namespace std;
#include <vector>
// vector赋值操作
void printVector(vector<int>& v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void testA()
{
vector<int> v1; //默认构造,无参构造
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
printVector(v1);
// 赋值 operator=
vector<int> v2 = v1;
printVector(v2);
// assign
vector<int> v3;
v3.assign(v1.begin(), v1.end());
printVector(v3);
vector<int> v4;
v4.assign(10, 100);
printVector(v4);
}
int main()
{
testA();
return 0;
}
vector容量和大小
功能描述:
- 对vector容器的容量和大小操作
函数原型:
empty();
//判断容器是否为空capacity();
//容器的容量size();
//返回容器中元素的个数resize(int num);
//重新指定容器的长度为num,若容器变长,则以默认值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。resize(int num, elem);
//重新指定容器的长度为num,若容器变长,则以elem值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。
示例:
#include <iostream>
#include <string>
using namespace std;
#include <vector>
// vector容量和大小
void printVector(vector<int>& v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void testA()
{
vector<int> v1; //默认构造,无参构造
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
printVector(v1);
if (v1.empty())
{
cout << "容器为空" << endl;
}
else {
cout << "容器不为空" << endl;
cout << "v1的容量为:" << v1.capacity() << endl;
cout << "v1的大小为:" << v1.size() << endl;
}
// 重新指定大小
v1.resize(15,100);//利用重载版本,可以指定默认填充, 参数2
//如果重新指定的比原来的长,默认用0填充
printVector(v1);
//指定的更小,超出部分元素被删除
v1.resize(5);
printVector(v1);
}
int main()
{
testA();
return 0;
}
vector插入和删除
功能描述:
- 对vector容器进行插入、删除操作
函数原型:
push_back(ele);
//尾部插入元素elepop_back();
//删除最后一个元素insert(const_iterator pos, ele);
//迭代器指向位置pos插入元素eleinsert(const_iterator pos, int count, ele);
//迭代器指向位置pos插入count个元素eleerase(const_iterator pos);
//删除迭代器指向的元素erase(const_iterator start, const_iterator end);
// 删除迭代器从start到end之间的元素clear();
//删除容器中所有元素
示例:
#include <iostream>
#include <string>
using namespace std;
#include <vector>
// vector插入和删除
void printVector(vector<int>& v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void testA()
{
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);
v1.clear();
printVector(v1);
}
int main()
{
testA();
return 0;
}
vector数据存取
功能描述:
- 对vector中的数据做存取操作
函数原型:
at(int idx);
//返回索引idx所指的数据operator[];
//返回索引idx所指的数据front();
//返回容器中第一个数据元素back();
//返回容器中最后一个数据元素
示例:
#include <iostream>
#include <string>
using namespace std;
#include <vector>
// vector数据存取
void printVector(vector<int>& v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void testA()
{
vector<int> v1; //默认构造,无参构造
for (int i = 0; i < 10; i++)
{
// 尾部插入
v1.push_back(i);
}
// operator=
cout << v1[1] << endl;
// at
cout << v1.at(2) << endl;
//获取第一个元素
cout << "第一个元素: " << v1.front() << endl;
//获取最后一个元素
cout << "最后一个元素: " << v1.back() << endl;
}
int main()
{
testA();
return 0;
}
vector互换容器
功能描述:
- 实现两个容器内元素进行互换
函数原型:
swap(vec);
//将vec与本身的元素互换
示例:
#include <iostream>
#include <string>
using namespace std;
#include <vector>
// vector容器互换
void printVector(vector<int>& v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void testA()
{
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 - 1; i >= 0; i--)
{
v2.push_back(i);
}
printVector(v2);
v1.swap(v2);
cout << "交换后: " << endl;
printVector(v1);
printVector(v2);
}
//实际用途
//巧用swap可以收缩内存空间
void testB()
{
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;
//收缩内存空间
vector<int>(v).swap(v);
cout << "v的容量为: " << v.capacity() << endl;
cout << "v的大小为: " << v.size() << endl;
}
int main()
{
testA();
testB();
return 0;
}
vector预留空间
功能描述:
- 减少vector在动态扩展容量是的扩展次数
函数原型:
reserve(int len);
//容器预留len个元素长度,预留位置不初始化,元素不可访问
示例:
#include <iostream>
#include <string>
using namespace std;
#include <vector>
// vector预留空间
void printVector(vector<int>& v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void testA()
{
vector<int> v;
size_t previousCapacity = v.capacity();
for (int i = 0; i < 1000000; i++)
{
v.push_back(i);
if (v.capacity() != previousCapacity) {
std::cout << "Vector 容量增加: " << previousCapacity << " -> " << v.capacity() << std::endl;
previousCapacity = v.capacity();
}
}
}
void testB()
{
vector<int> v;
//预留空间
v.reserve(100000);
int* p = NULL;
int num = 0;//计算动态扩展空间的次数
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()
{
testB();
return 0;
}
deque容器
deque容器基本概念
功能:
- 双端数组,可以对头端进行插入删除操作
deque与vector区别:
- vector对于头部的插入删除效率低,数据量越大,效率越低
- deque相对而言,对头部的插入删除速度会比vector快
- vector访问元素时的速度会比deque快,这和两者内部实现有关
deque内部工作原理:
deque内部有个中控器,维护每段缓冲区的内容,缓冲区中存放真实数据
中控器维护的是每个缓冲区的地址,使得使用deque时像一片连续的内存空间
- deque容器的迭代器也是支持随机访问的
deque构造函数
功能描述:
- deque容器构造
函数原型:
deque<T> deqT;
// 默认构造函数deque(beg, end);
//构造函数将[beg,end]区间中的元素拷贝给本身。deque(n, elem);
//构造函数将n个elem拷贝给本身。deque(const deque &deq);
//拷贝构造函数
示例:
#include <iostream>
#include <string>
using namespace std;
#include <deque>
void printDeque(const deque<int>& d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
//*it = 100; 容器中的数据不可被修改
cout << *it << " ";
}
cout << endl;
}
void testA()
{
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(d4);
}
int main()
{
testA();
return 0;
}
deque赋值操作
功能描述:
- 给deque容器进行赋值
函数原型:
deque& operator=(const deque &deq);
//重载等号操作符assign(beg, end);
//将[beg,end]区间中的数据拷贝赋值给本身assign(n, elem);
//将n个elem拷贝赋值给本身
示例:
#include <iostream>
#include <string>
using namespace std;
#include <deque>
void printDeque(const deque<int>& d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void testA()
{
deque<int> d1;
for (int i = 0; i < 10; i++)
{
d1.push_back(i);
}
printDeque(d1);
// operator=赋值
deque<int> d2;
d2 = d1;
printDeque(d2);
//assign赋值
deque<int> d3;
d3.assign(d2.begin(), d2.end());
printDeque(d3);
deque<int> d4;
d4.assign(10, 100);
printDeque(d4);
}
int main()
{
testA();
return 0;
}
deque容器大小操作
功能描述:
- 对deque容器的大小进行操作
函数原型:
deque.empty();
//判断容器是否为空deque.size();
//返回容器中元素的个数deque.resize(num);
//重新指定容器的长度为num,若容器变长,则以默认值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。deque.resize(num, elem);
//重新指定容器的长度为num,若容器变长,则以elem值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。
示例:
#include <iostream>
#include <string>
using namespace std;
#include <deque>
void printDeque(const deque<int>& d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void testA()
{
deque<int> d1;
for (int i = 0; i < 10; i++)
{
d1.push_back(i);
}
if (d1.empty())
{
cout << "空的" << endl;
}
else {
printDeque(d1);
}
cout << "容器大小为: " << d1.size() << endl;
//重新指定大小
d1.resize(15);
printDeque(d1);
d1.resize(20, 1);
printDeque(d1);
d1.resize(5);
printDeque(d1);
}
int main()
{
testA();
return 0;
}
deque的插入和删除
功能描述:
- 向deque容器中插入和删除数据
函数原型:
两端插入操作:
push_back(elem);
//尾插push_front(elem);
//头插pop_back();
//尾删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位置的数据,返回下一个数据位置
示例:
#include <iostream>
#include <string>
using namespace std;
#include <deque>
void printDeque(const deque<int>& d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void testA()
{
deque<int> d1;
//尾插
d1.push_back(10);
d1.push_back(20);
//头插
d1.push_front(100);
d1.push_front(200);
printDeque(d1);
//尾删
d1.pop_back();
//头删
d1.pop_front();
printDeque(d1);
d1.insert(d1.begin(), 30);
printDeque(d1);
d1.insert(d1.begin(), 2, 40);
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);
//删除
d1.erase(d1.begin());
printDeque(d1);
d2.erase(d2.begin(), d2.end());
printDeque(d2);
//清空
d1.clear();
printDeque(d1);
}
int main()
{
testA();
return 0;
}
deque数据存取
功能描述:
- 对deque中的数据的存取操作
函数原型:
at(int idx);
//返回索引idx所指的数据operator[];
//返回索引idx所指的数据front();
//返回容器第一个数据元素back();
//返回容器最后一个数据元素
示例:
#include <iostream>
#include <string>
using namespace std;
#include <deque>
void testA()
{
deque<int> d1;
d1.push_back(10);
d1.push_back(20);
cout << d1.at(0) << endl;
cout << d1[1] << endl;
cout << d1.front() << endl;
cout << d1.back() << endl;
}
int main()
{
testA();
return 0;
}
deque数据排序
功能描述:
- 利用算法实现对deque容器进行排序
算法:
sort(iterator beg, iterator end);
//对beg和end区间内元素进行排序
#include <iostream>
#include <string>
using namespace std;
#include <deque>
#include <algorithm>
void printDeque(const deque<int>& d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void testA()
{
deque<int> d1;
d1.push_back(1);
d1.push_back(3);
d1.push_back(5);
d1.push_back(7);
d1.push_back(9);
d1.push_back(2);
d1.push_back(4);
d1.push_back(6);
d1.push_back(8);
//排序
sort(d1.begin(), d1.end());
printDeque(d1);
}
int main()
{
testA();
return 0;
}
案例-评委打分
案例描述
有五名选手:选手ABCDE,10个评委分别对每一名选手打分,去除最高分,去除评委中最低分,取平均数。
案例步骤
- 创建五名选手,放到vector容器中。
- 遍历vector容器,取出来每一个选手执行for循环。可以把十个打分存到deque容器中
- sort算法对deque容器中分数排序,去除最高分和最低分
- deque容器遍历一遍,累加总分
- 获取平均分
示例:
#include <iostream>
using namespace std;
#include <string>
#include <vector>
#include <deque>
#include <algorithm>
#include <ctime>
class Person
{
public:
string m_name;
int m_score;
public:
Person(string name, int score)
{
this->m_name = name;
this->m_score = 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);
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 scroe = rand() % 41 + 60;
d.push_back(scroe);
}
//排序
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;
}
}
int main()
{
//随机数种子
srand((unsigned int)time(NULL));
//1.创建5名选手
vector<Person> v;
createPerson(v);
//2.给5名选手打分
setScore(v);
//3.显示最后得分
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
{
cout << "姓名: " << it->m_name << " 分数: " << it->m_score << endl;
}
return 0;
}
stack容器(栈)
stack基本概念
概念:stack是一种先进后出(First In Last Out,FILO)的数据结构,它只有一个出口
栈中只有顶部的元素才可以被外界使用,因此栈不允许有遍历行为
stack常用接口
功能描述:栈容器常用的对外接口
构造函数:
stack<T> stk;
//stack采用模板类实现, stack对象的默认构造函数形式stack(const stack &stk);
//拷贝构造函数
赋值操作:
stack& operator=(const stack &stk);
//重载等号操作符
数据存取:
push(elem);
//向栈顶添加元素pop();
//从栈顶移除第一个元素top();
//返回栈顶元素
大小操作:
empty();
//判断栈是否为空size();
//返回栈大小
示例:
#include <iostream>
using namespace std;
#include <string>
#include <stack>
void testA()
{
stack<int> stk;
stk.push(10);
stk.push(20);
stk.push(30);
stk.push(40);
cout << "栈大小为: " << stk.size() << endl;
//只要栈不为空,查看栈顶,并执行出栈操作
while (!stk.empty())
{
//栈顶元素
cout << stk.top() << endl;
//出栈
stk.pop();
}
cout << "栈大小为: " << stk.size() << endl;
}
int main()
{
testA();
return 0;
}
queque容器(队列)
queue基本概念
概念: Queue是一种先进选出(First In Out,FIFO)的数据结构,它有两个出口
队列容器允许从一端新增数据,从另一端移除元素
队列中只有队头和队尾才可以被外界使用, 因此队列不允许有遍历行为
队列中进数据称为 --- 入队 push
队列中出数据称为 --- 出队 pop
queue常用接口
功能描述:
queue<T> que;
//默认构造queue(const queue &que);
// 拷贝构造
赋值操作:
queue& operator=(const queue &que);
//重载等号操作符
数据存取:
push(elem);
//队尾添加元素pop();
//队头删除第一个元素back();
//返回最后一个元素front();
//返回第一个元素
大小操作:
- empty();` //判断队列是否为空
size();
//返回队列大小
示例:
#include <iostream>
using namespace std;
#include <string>
#include <queue>
void testA()
{
queue<int> que;
que.push(10);
que.push(20);
que.push(30);
que.push(40);
//最后一个元素
cout << "第一个元素: " << que.front() << endl;
cout << "最后一个元素: " << que.back() << endl;
cout << "栈大小为: " << que.size() << endl;
//只要队列不为空,查看队里,并执行出队操作
while (!que.empty())
{
//栈顶元素
cout << que.front() << endl;
//出栈
que.pop();
}
cout << "栈大小为: " << que.size() << endl;
}
int main()
{
testA();
return 0;
}
list容器(链表)
list基本概念
功能: 将数据进行链式存储
链表: (list)是一种物理存储单元上非连续的存储结构, 数据元素的逻辑顺序是通过链表中的指针链接实现的
链表的组成: 链表由一系列节点组成
节点的组成: 一个是存储数据元素的数据域, 另一个是存储下一个节点地址的指针域
STL中的链表是一个双向循环链表
由于链表的存储方式并不是连续的内存空间, 因此链表list中的迭代器只支持前移后移, 属于双向迭代器
优点: 采用动态存储分配,不会造成内存浪费和溢出。可以对任意位置进行快速的插入和删除
缺点: 链表灵活,但是空间(指针域)和时间(遍历)额外开销较大。容器遍历速度, 没有数组快, 占用空间比数组大
list有一个重用的性质, 插入操作和删除操作都不会造成原有list迭代器的失效,这在vector中是不成立。
总结:STL中List和vector是连个最常被使用的容器,各有优缺点
list构造函数
功能描述: 创建list容器
函数原型:
list<T> lst;
//默认构造list(beg,end);
//构造函数将[beg,end]区间的元素拷贝给本身list(n,elem);
//构造函数将n个elem拷贝给本身list(const list &lst);
//拷贝构造函数
示例:
#include <iostream>
using namespace std;
#include <string>
#include <list>
void printList(list<int>& li)
{
for (list<int>::iterator it = li.begin(); it != li.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void testA()
{
list<int> li;
//添加数据
li.push_back(10);
li.push_back(20);
li.push_back(30);
li.push_back(40);
//遍历容器
printList(li);
//区间构造
list<int> l2(li.begin(), li.end());
printList(l2);
//拷贝构造
list<int>l3(l2);
printList(l3);
list<int> l4(4, 1000);
printList(l4);
}
int main()
{
testA();
return 0;
}
list赋值和交换
功能描述: 给list容器进行赋值,以及交换list容器
函数原型:
assign(beg,end);
//将[beg,end]区间的元素拷贝给本身assign(n, elem);
// 将n个elem拷贝赋值给本身list& operator=(const list &lst);
//重载等号重载符swap(lst);
//将lst与本身的元素互换
示例:
#include <iostream>
using namespace std;
#include <string>
#include <list>
void printList(list<int>& li)
{
for (list<int>::iterator it = li.begin(); it != li.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void testA()
{
list<int> li;
//添加数据
li.push_back(10);
li.push_back(20);
li.push_back(30);
li.push_back(40);
//遍历容器
printList(li);
list<int> l2;
l2.assign(li.begin(), li.end());
printList(l2);
list<int> l3;
l3.assign(4, 100);
printList(l3);
list<int> l4;
l4 = l3; // operator=
printList(l4);
}
//交换
void testB()
{
list<int> li;
//添加数据
li.push_back(10);
li.push_back(20);
li.push_back(30);
li.push_back(40);
list<int> l2;
l2.assign(10, 100);
cout << "交换前: " << endl;
printList(li);
printList(l2);
cout << "交换后: " << endl;
li.swap(l2);
printList(li);
printList(l2);
}
int main()
{
testA();
testB();
return 0;
}
list大小操作
功能描述: 对list容器的大小进行操作
函数原型:
size();
//返回容器元素个数empty();
//判断容器是否为空resize(num);
//重新指定容器长度为num,若容器变长,则以默认值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除resize(num,elem);
////重新指定容器长度为num,若容器变长,则以elem值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除
示例:
#include <iostream>
using namespace std;
#include <string>
#include <list>
void printList(list<int>& li)
{
for (list<int>::iterator it = li.begin(); it != li.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void testA()
{
list<int> li;
//添加数据
li.push_back(10);
li.push_back(20);
li.push_back(30);
li.push_back(40);
//判断容器是否为空
if (li.empty())
{
cout << "容器为空" << endl;
}
else
{
cout << "元素个数: " << li.size() << endl;
//遍历容器
printList(li);
}
//重新指定大小
li.resize(10, 1000);
printList(li);
li.resize(5);
printList(li);
}
int main()
{
testA();
return 0;
}
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);
//在post位置插入[beg,end]区间的数据,无返回值clear();
//清空容器数据erase(beg,end);
//删除[beg,end]取的数据,返回下一个数据位置erase(pos);
//删除pos位置的数据,返回下一个数据位置remove(elem);
//删除容器中所有与elem值匹配的元素。
示例:
#include <iostream>
using namespace std;
#include <string>
#include <list>
void printList(const list<int>& li)
{
for (list<int>::const_iterator it = li.begin(); it != li.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void testA()
{
list<int> li;
//尾插
li.push_back(10);
li.push_back(20);
li.push_back(30);
li.push_back(40);
//头插
li.push_front(100);
li.push_front(200);
printList(li);
//尾删
li.pop_back();
//头删
li.pop_front();
printList(li);
//insert()插入
li.insert(li.begin(), 1000);
li.insert(li.begin(), 2, 10000);
printList(li);
//删除
li.erase(li.begin());
printList(li);
//移除
li.remove(1000);
printList(li);
//清空
li.clear();
printList(li);
}
int main()
{
testA();
return 0;
}
list数据存取
功能描述:
- 对list容器中数据进行存取
函数原型:
front();
//返回第一个元素back();
//返回最后一个元素
示例:
#include <iostream>
using namespace std;
#include <string>
#include <list>
void testA()
{
list<int> li;
//尾插
li.push_back(10);
li.push_back(20);
li.push_back(30);
li.push_back(40);
cout << li.front() << endl;
cout << li.back() << endl;
}
int main()
{
testA();
return 0;
}
list反转和排序
功能描述:
- 将容器中的元素反转,以及将容器中的数据进行排序
函数原型:
reverse();
//反转链表sort();
//链表排序
示例:
#include <iostream>
using namespace std;
#include <string>
#include <list>
#include <algorithm>
void printList(const list<int>& li)
{
for (list<int>::const_iterator it = li.begin(); it != li.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void testA()
{
list<int> li;
//尾插
li.push_back(20);
li.push_back(10);
li.push_back(40);
li.push_back(30);
printList(li);
//反转
li.reverse();
printList(li);
//排序
li.sort(); //默认升序
printList(li);
//倒叙
li.sort([](int v1, int v2)
{
return v1 > v2;
});
printList(li);
//所有不支持随机访问迭代器的容器,不支持标准算法
//sort(li.begin(), li.end());
}
int main()
{
testA();
return 0;
}
排序案例
案例描述:将Person自定义数据类型进行排序,Person中属性有姓名、年龄、身高
排序规则:按照年龄进行升序,如果年龄相同按照身高进行降序
示例:
#include <iostream>
using namespace std;
#include <string>
#include <list>
class Person
{
public:
string m_name;//姓名
int m_age;//年龄
int m_height;//身高
Person(string name, int age, int height)
{
this->m_age = age;
this->m_height = height;
this->m_name = name;
}
};
void printList(const list<Person>& li)
{
for (list<Person>::const_iterator it = li.begin(); it != li.end(); it++)
{
cout << "姓名:" << it->m_name << " 年龄: " << it->m_age << " 身高: " << it->m_height << endl;
}
}
bool comparePerson(Person& p1, Person& p2)
{
if (p1.m_age == p2.m_age)
{
return p1.m_height > p2.m_height;
}
else {
// 按照年龄升序
return p1.m_age < p2.m_age;
}
}
void testA()
{
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);
printList(L);
//排序后
cout << "排序后: " << endl;
L.sort(comparePerson);
printList(L);
}
int main()
{
testA();
return 0;
}
set/multiset容器(集合)
set基本概念
简介:
- 所有元素都会在插入式自动被排序
本质:
- set/multiset属于关联式容器,底层结构式用二叉树实现
set和multiset区别:
- set不允许容器中有重复的元素
- multiset允许容器中有重复元素
set构造和赋值
功能描述: 创建set容器以及赋值
构造:
set<T> st;
//默认构造set(const set &st);
//拷贝构造
赋值:
set& operator=(const set &st);
//重载等号操作符
示例:
#include <iostream>
using namespace std;
#include <string>
#include <set>
void printSet(const set<int>& li)
{
for (set<int>::const_iterator it = li.begin(); it != li.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void testA()
{
set<int> s1;
//插入数据 只有insert方式
s1.insert(10);
s1.insert(20);
s1.insert(30);
s1.insert(30);
s1.insert(40);
//插入元素会自动排序, 且set容器不允许有重复值
printSet(s1);
//拷贝构造
set<int>s2(s1);
printSet(s2);
//赋值
set<int>s3;
s3 = s1;
printSet(s3);
}
int main()
{
testA();
return 0;
}
set大小和交换
功能描述:
- 统计set容器大小以及交换set容器
函数原型:
size();
//返回容器大小empty();
//判断容器是否为空swap();
//交换两个集合容器
示例:
#include <iostream>
using namespace std;
#include <string>
#include <set>
void printSet(const set<int>& st)
{
for (set<int>::const_iterator it = st.begin(); it != st.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void testA()
{
set<int> s1;
//插入数据 只有insert方式
s1.insert(10);
s1.insert(20);
s1.insert(30);
s1.insert(30);
s1.insert(40);
if (s1.empty())
{
cout << "容器为空" << endl;
}
else {
//插入元素会自动排序, 且set容器不允许有重复值
printSet(s1);
cout << "容器大小为: " << s1.size() << endl;
}
set<int> s2;
s2.insert(100);
s2.insert(200);
s2.insert(300);
s2.insert(400);
cout << "交换前: " << endl;
printSet(s1);
printSet(s2);
cout << "交换后: " << endl;
s1.swap(s2);
printSet(s1);
printSet(s2);
}
int main()
{
testA();
return 0;
}
set插入和删除
功能描述:
- set容器进行插入和删除数据操作
函数原型:
insert(elem);
//向容器插入数据clear();
//清空容器erase(pos);
//删除pos迭代器所指的数据,返回下一个元素的迭代器erase(beg,end);
//删除[beg,end]区间的数据,返回下一个元素的迭代器erase(elem);
//删除容器中值为elem的元素
示例:
#include <iostream>
using namespace std;
#include <string>
#include <set>
void printSet(const set<int>& st)
{
for (set<int>::const_iterator it = st.begin(); it != st.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void testA()
{
set<int> s1;
//插入数据 只有insert方式
s1.insert(10);
s1.insert(20);
s1.insert(30);
s1.insert(30);
s1.insert(40);
printSet(s1);
// 删除
s1.erase(30);
printSet(s1);
s1.erase(s1.begin());
printSet(s1);
// 清空
s1.clear();
printSet(s1);
}
int main()
{
testA();
return 0;
}
set查找和统计
功能描述:
- 对set容器进行查找和统计数据
函数原型:
find(key);
//查找key是否存在,若存在,返回该键的元素的迭代器,若不存在,返回set.end();count();
//统计key元素个数
示例:
#include <iostream>
using namespace std;
#include <string>
#include <set>
void testA()
{
set<int> s1;
//插入数据 只有insert方式
s1.insert(10);
s1.insert(20);
s1.insert(30);
s1.insert(30);
s1.insert(40);
//查找
set<int>::iterator pos = s1.find(30);
if (pos != s1.end())
{
cout << *pos << endl;
}
else {
cout << "没找到元素" << endl;
}
//统计
//对与set而言,统计结果要么是0 要么是1
cout << s1.count(30) << endl;
}
int main()
{
testA();
return 0;
}
set和multiset区别
学习目标:
- 掌握set和multiset的区别
区别:
- set不可以插入重复数据,而multiset可以
- set插入数据的同时会返回插入结果,表示插入是否成功
- multiset不会检测数据,因此可以插入重复数据
示例:
#include <iostream>
using namespace std;
#include <string>
#include <set>
void printSet(const multiset<int>& st)
{
for (set<int>::const_iterator it = st.begin(); it != st.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void testA()
{
set<int> s1;
//插入数据 只有insert方式
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;
}
multiset<int> s2;
s2.insert(10);
s2.insert(10);
s2.insert(10);
s2.insert(10);
printSet(s2);
}
int main()
{
testA();
return 0;
}
pair对组创建
功能描述:
- 成对出现的数据,利用对组可以返回两个数据
两种创建方式:
pair<type,type> p(value1,value2);
pair<type,type> p = make_pair(value1,value2);
示例:
#include <iostream>
using namespace std;
#include <string>
void testA()
{
//第一种方式
pair<string, int> p("Tom", 20);
cout << "姓名: " << p.first << " 年龄: " << p.second << endl;
//第二种
pair<string, int>p2 = make_pair("Jerry", 30);
cout << "姓名: " << p2.first << " 年龄: " << p2.second << endl;
}
int main()
{
testA();
return 0;
}
set容器排序
学习目标:
- set容器默认排序规则为从小到大,掌握如何改变排序规则
主要技术的:
- 利用仿函数,可以改变排序规则
示例一 set存放内置数据类型
#include <iostream>
using namespace std;
#include <string>
#include <set>
class MyCompare
{
public:
bool operator()(int v1,int v2) const
{
return v1 > v2;
}
};
void testA()
{
set<int> s1;
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(50);
s1.insert(40);
for (set<int>::iterator it = s1.begin(); it != s1.end(); it++)
{
cout << *it << " ";
}
cout << endl;
set<int,MyCompare> s2;
s2.insert(10);
s2.insert(30);
s2.insert(20);
s2.insert(50);
s2.insert(40);
for (set<int,MyCompare>::iterator it = s2.begin(); it != s2.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
testA();
return 0;
}
示例二 set存放自定义数据类型
#include <iostream>
using namespace std;
#include <string>
#include <set>
class Person
{
public:
string m_name;
int m_age;
Person(string name, int age)
{
this->m_age = age;
this->m_name = name;
}
};
class MyCompare
{
public:
bool operator()(const Person &v1, const Person &v2) const
{
return v1.m_age > v2.m_age;
}
};
void testA()
{
set<Person,MyCompare> 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, MyCompare>::iterator it = s.begin(); it != s.end(); it++)
{
cout << "姓名: " << it->m_name << " 年龄: " << it->m_age << endl;
}
}
int main()
{
testA();
return 0;
}
map/multimap容器
map基本概念
简介:
- map中所有元素都是pair
- pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值)
- 所有元素都会根据元素的键值自动排序
本质:
- map/multimap属于关联式容器,底层结果是用二叉树实现
优点:
- 可以根据key值快速找到value值
map和multimap区别:
- map不允许容器中有重复key值元素
- multimap允许容器中有重复key值元素
map构造和赋值
功能描述:
- 对map容器进行构造和赋值操作
构造:
map<T1,T2> mp;
//默认构造map(const map &mp);
//拷贝构造
赋值:
map& operator=(const map &mp);
//重载等号操作符
示例:
#include <iostream>
using namespace std;
#include <string>
#include <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 testA()
{
map<int, int> m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(3, 30));
m.insert(pair<int, int>(2, 20));
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()
{
testA();
return 0;
}
map大小和交换
功能描述:
- 统计map容器大小以及交换map容器
函数原型:
size();
//返回容器中元素个数empty();
//判断容器是否为空swap();
//交换两个map容器
示例:
#include <iostream>
using namespace std;
#include <string>
#include <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 testA()
{
map<int, int> m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(3, 30));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(4, 40));
if (m.empty())
{
cout << "空的" << endl;
}
else {
cout << "容器大小为: " << m.size() << endl;
}
//交换容器
map<int, int> m1;
m1.insert(pair<int, int>(5, 10));
m1.insert(pair<int, int>(6, 30));
m1.insert(pair<int, int>(7, 20));
m1.insert(pair<int, int>(8, 40));
cout << "交换前: " << endl;
printMap(m);
printMap(m1);
cout << "交换前: " << endl;
m.swap(m1);
printMap(m);
printMap(m1);
}
int main()
{
testA();
return 0;
}
map插入和删除
功能描述:
- map容器进行插入数据和删除数据
函数原型:
insert(ele);
//向容器插入数据clear();
//清空容器erase(pos);
//删除pos迭代器所指的元素,返回下一个元素的迭代器erase(beg,end);
//删除[beg,end]区间的所有元素,返回下一个元素的迭代器erase(key);
//删除容器中值为key的元素
示例:
#include <iostream>
using namespace std;
#include <string>
#include <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 testA()
{
map<int, int> m;
//插入
//第一种
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(3, 30));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(4, 40));
//第二种
m.insert(make_pair(5, 50));
//第三种
m.insert(map<int, int>::value_type(6, 60));
//第四种
m[7] = 70;
//删除
m.erase(m.begin());
m.erase(4);
printMap(m);
//清空
m.erase(m.begin(), m.end());
m.clear();
printMap(m);
}
int main()
{
testA();
return 0;
}
map查找和统计
功能描述:
- 对map容器进行查找数据以及统计数据
函数原型:
find(key);
//查找key是否存在,若存在,返回该键的元素的迭代器,若不存在,返回map.end();count(key);
//统计key的元素个数
示例:
#include <iostream>
using namespace std;
#include <string>
#include <map>
void testA()
{
map<int, int> m;
//插入
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(3, 30));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(4, 40));
map<int, int>::iterator it = m.find(3);
if (it != m.end())
{
cout << "key: " << it->first << " value: " << it->second << endl;
}
else {
cout << "没找到" << endl;
}
//对于map来说,要么是1,要么是0,map不允许插入重复的key
cout << m.count(2) << endl;
multimap<int, int> m1;
m1.insert(pair<int, int>(1, 10));
m1.insert(pair<int, int>(1, 10));
cout << m1.count(1) << endl;
}
int main()
{
testA();
return 0;
}
map容器排序
学习目标:
- map容器默认排序规则为: 按照key值进行从大到小排序,掌握如何改变排序规则
主要技术点:
- 利用仿函数,可以改变排序规则
示例:
#include <iostream>
using namespace std;
#include <string>
#include <map>
class MyCompare
{
public:
bool operator()(int v1, int v2) const
{
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 testA()
{
map<int, int, MyCompare> m;
//插入
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(3, 30));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(4, 40));
cout << "排序后:" << endl;
printMap(m);
}
int main()
{
testA();
return 0;
}
案例-员工分组
案例描述
- 是今天招聘了十个员工(ABCDEFGHIJ),10名员工进入公司之后,需要指派员工在哪个部门工作
- 员工信息有:姓名,工资组成,部门分为:策划,美术,研发
- 随机给十名员工分配部门和工资。
- 通过multimap进行信息的插入key(部门编号)value(员工)
- 分部门显示员工信息
实现步骤
- 创建10名员工,放到vector中
- 遍历vector容器,取出每个员工,进行随机分组
- 分组后将员工部门编号作为key,具体员工作为value,放入到multimap容器中
- 分部门显示员工信息
案例代码:
#include <iostream>
using namespace std;
#include <string>
#include <vector>
#include <map>;
#define CHEHUA 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; //10000~19999
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;//0 1 2
m.insert(make_pair(deptId, *it));
}
}
void showWorkerByGroup(multimap<int, Worker>& m)
{
cout << "策划部门:" << endl;
multimap<int, Worker>::iterator pos = m.find(CHEHUA);
int count = m.count(CHEHUA);
int index = 0;
for (; pos != m.end()&&index < count; pos++,index++)
{
cout << "姓名:" << pos->second.m_name << " 工资: " << pos->second.m_salary << 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_salary << 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_salary << endl;
}
}
void printWorker(vector<Worker>& v)
{
for (vector<Worker>::iterator it = v.begin(); it != v.end(); it++)
{
cout << "姓名:" << it->m_name << " 工资: " << it->m_salary << endl;
}
}
int main()
{
srand((unsigned int)time(NULL));
//1.创建10名员工,放到vector中
vector<Worker> vWorker;
createWorker(vWorker);
//printWorker(vWorker);
//2.遍历vector容器,取出每个员工,进行随机分组
//3.分组后将员工部门编号作为key,具体员工作为value,放入到multimap容器中
multimap<int, Worker> mWorker;
setGroup(vWorker, mWorker);
//4.分部门显示员工信息
showWorkerByGroup(mWorker);
return 0;
}