本系列文章为黑马程序员C++教程学习笔记,前面的系列文章链接如下
C++核心编程:P1->程序的内存模型
C++核心编程:P2->引用
C++核心编程:P3->函数提高
C++核心编程:P4->类和对象----封装
C++核心编程:P5->类和对象----对象的初始化和清理
C++核心编程:P6->类和对象----C++对象模型和this指针
C++核心编程:P7->类和对象----友元
C++核心编程:P8->类和对象----运算符重载
C++核心编程:P9->类和对象----继承
C++核心编程:P10->类和对象----多态
C++核心编程:P11->文件操作
C++核心编程:P12->模板----函数模板
C++核心编程:P13->模板----类模板
C++核心编程:P14->STL----STL初识
- 一、string容器
- 1.1 string基本概念
- 1.2 string构造函数
- 1.3 string赋值操作
- 1.4 string字符串拼接
- 1.5 string查找和替换
- 1.6 string字符串比较
- 1.7 string字符存取
- 1.8 string插入和删除
- 1.9 string子串
- 二、vector容器
- 2.1 vector基本概念
- 2.2 vector构造函数
- 2.3 vector赋值操作
- 2.4 vector容量和大小
- 2.5 vector插入和删除
- 2.6 vector数据存取
- 2.7 vector互换容器
- 2.8 vector预留空间
- 三、deque容器
- 3.1 deque容器基本概念
- 3.2 deque构造函数
- 3.3 deque赋值操作
- 3.4 deque大小操作
- 3.5 deque插入和删除
- 3.6 deque数据存取
- 3.7 deque排序
- 四、STL案例----评委打分
一、string容器 1.1 string基本概念
本质: string是C++风格的字符串,而string本质上是一个类
string和char * 区别:
①char *是一个指针
②string是一个类,类内部封装了char*,管理这个字符串,是一个char*型的容器。
特点:
①string类内部封装了很多成员方法。例如:查找find,拷贝copy,删除delete 替换replace,插入insert
②string管理char*所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责
1.2 string构造函数
string(); //创建一个空的字符串 例如: string str;
string(const char* s); //使用字符串s初始化
string(const string& str); //使用一个string对象初始化另一个string对象
string(int n, char c); //使用n个字符c初始化
案例: 使用四种构造函数来实例化string对象。
#includeusing namespace std; #include void test01() { string s1; //创建空字符串,调用无参构造函数 const char* s = "hello world"; string s2(s); //把c_string转换成了string cout << "s2 = " << s2 << endl; string s3(s2); //调用拷贝构造函数 cout << "s3 = " << s3 << endl; string s4(10, 'a'); //使用n个字符a初始化字符串 cout << "s4 = " << s4 << endl; } int main() { test01(); system("pause"); return 0; }
运行,结果如下。
1.3 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赋给当前字符串
案例: 使用上述7种赋值函数来进行赋值操作。
#includeusing namespace std; #include void test01() { string s1; s1 = "hello world"; cout << "s1 = " << s1 << endl; string s2; s2 = s1; cout << "s2 = " << s2 << endl; string s3; s3 = 'a'; cout << "s3 = " << s3 << endl; string s4; s4.assign("hello world"); cout << "s4 = " << s4 << endl; string s5; s5.assign("hello world", 6); cout << "s5 = " << s5 << endl; string s6; s6.assign(s5); cout << "s6 = " << s6 << endl; string s7; s7.assign(10, 'w'); cout << "s7 = " << s7 << endl; } int main() { test01(); system("pause"); return 0; }
运行,结果如下。
1.4 string字符串拼接
功能描述: 实现在字符串末尾拼接字符串
函数原型:
string& operator+=(const char* str); //重载+=操作符
string& operator+=(const char c); //重载+=操作符
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个字符连接到字符串结尾
案例: 测试字符串拼接的各种方式
#includeusing namespace std; #include void test01() { string str1 = "我"; str1 += "爱玩游戏"; //"我爱玩游戏" cout << "str1 = " << str1 << endl; str1 += ':'; //"我爱玩游戏:" cout << "str1 = " << str1 << endl; string str2 = "LOL DNF"; str1 += str2; //"我爱玩游戏:LOL DNF" cout << "str1 = " << str1 << endl; string str3 = "I"; str3.append(" love "); //"I love " cout << "str3 = " << str3 << endl; str3.append("game abcd", 4); //"I love game" cout << "str3 = " << str3 << endl; str3.append(str2); //"I love gameLOL DNF" cout << "str3 = " << str3 << endl; str3.append(str2, 4, 3); //"I love gameLOL DNFDNF" cout << "str3 = " << str3 << endl; } int main() { test01(); system("pause"); return 0; }
运行,效果如下
1.5 string查找和替换
功能描述:
查找:查找指定字符串是否存在
替换:在指定的位置替换字符串
函数原型:
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(const 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
注意:
find查找是从左往后,rfind从右往左
find找到字符串后返回查找的第一个字符位置,找不到返回-1
replace在替换时,要指定从哪个位置起,多少个字符,替换成什么样的字符串
案例: 测试字符串的查找与替换功能
#includeusing namespace std; #include void test01() { cout << "字符串查找" << endl; string str1 = "abcdefgde"; int pos; pos = str1.find("de"); cout << "de位置在:" << pos << endl; pos = str1.find("UI"); cout << "UI位置在:" << pos << endl; pos = str1.rfind("de"); cout << "de位置在:" << pos << endl; cout << "字符串替换" << endl; str1.replace(1, 3, "1111"); cout << "str1 = " << str1 << endl; } int main() { test01(); system("pause"); return 0; }
运行,效果如下
1.6 string字符串比较
功能描述: 字符串之间的比较。主要是用于比较两个字符串是否相等,判断谁大谁小的意义并不是很大。
比较方式: 按字符的ASCII码逐个进行对比
=:两个字符串完全相等,返回0
>:第一个字符串的首个字符大于第二个字符串的首个字符,返回1
<:第一个字符串的首个字符小于第二个字符串的首个字符,返回-1
函数原型:
int compare(const string &s) const; //与字符串s比较
int compare(const char *s) const; //与字符串s比较
案例: 测试字符串的比较功能
#includeusing namespace std; #include void test01() { string str1 = "hello"; string str2 = "xello"; if (str1.compare(str2) == 0) { cout << "两个字符串相对" << endl; } else if (str1.compare(str2) > 0) { cout << "第1个字符串大于第2个字符串" << endl; } else { cout << "第1个字符串小于第2个字符串" << endl; } } int main() { test01(); system("pause"); return 0; }
运行,效果如下
1.7 string字符存取
string中单个字符存取方式有两种:
char& operator[](int n); //通过[]方式取字符
char& at(int n); //通过at方法获取字符
案例: 测试字符串的存取功能
#includeusing namespace std; #include void test01() { //读取 string str = "hello"; for (int i = 0; i < str.size(); i++) { cout << str[i] << " "; } cout << endl; for (int i = 0; i < str.size(); i++) { cout << str.at(i) << " "; } cout << endl; //修改 str[0] = 'x'; str.at(1) = 'x'; cout << "str = " << str << endl; } int main() { test01(); system("pause"); return 0; }
运行,效果如下
1.8 string插入和删除
功能描述: 对string字符串进行插入和删除字符操作
函数原型:
string& insert(int pos, const char* s); //插入字符串
string& insert(int pos, const string& str); //插入字符串
string& insert(int pos, int n, char c); //在指定位置插入n个字符c
string& erase(int pos, int n = npos); //删除从Pos开始的n个字符
案例: 测试字符串的插入和删除功能
#includeusing namespace std; #include void test01() { //插入 string str = "hello"; str.insert(1, "111"); cout << "str = " << str << endl; string str2 = "222"; str.insert(1, str2); cout << "str = " << str << endl; //删除 str.erase(2, 4); cout << "str = " << str << endl; } int main() { test01(); system("pause"); return 0; }
运行,效果如下
1.9 string子串
功能描述: 从字符串中获取想要的子串
函数原型: string substr(int pos = 0, int n = npos) const; //返回由pos开始的n个字符组成的字符串
案例: 测试字符串子串的功能
#includeusing namespace std; #include void test01() { string str = "abcdefg"; string substr = str.substr(1, 3); cout << "子串:" << substr << endl; string email = "zhangsan@weibo"; int pos = email.find('@'); substr = email.substr(0, pos); cout << "用户名:" << substr << endl; } int main() { test01(); system("pause"); return 0; }
运行,效果如下
二、vector容器 2.1 vector基本概念
功能: vector数据结构和数组非常相似,也称为单端数组
vector与普通数组区别: 不同之处在于数组是静态空间,而vector可以动态扩展
动态扩展: 并不是在原空间之后续接新空间,而是找更大的内存空间,然后将原数据拷贝新空间,释放原空间。
vector容器的迭代器是支持随机访问的迭代器,即可以跳跃式地访问容器元素,而不必逐个访问。
2.2 vector构造函数
功能描述: 创建vector容器
函数原型:
vectorv; //采用模板实现类实现,默认构造函数
vector(v.begin(), v.end()); //将v[begin(), end())区间中的元素拷贝给本身。
vector(n, elem); //构造函数将n个elem拷贝给本身。
vector(const vector &vec); //拷贝构造函数。
案例: 测试vector的几种构造函数
#includeusing namespace std; #include void printVector(vector v) { for (vector ::iterator it = v.begin(); it < v.end(); it++) { cout << *it << " "; } cout << endl; } void test01() { vector v1; for (int i = 0; i < 10; i++) { v1.push_back(i); } printVector(v1); vector v2(v1.begin(), v1.end()); printVector(v2); vector v3(10, 100); printVector(v3); vector v4(v3); printVector(v4); } int main() { test01(); system("pause"); return 0; }
运行,效果如下
2.3 vector赋值操作
功能描述: 给vector容器进行赋值
函数原型:
vector& operator=(const vector &vec);//重载等号操作符
assign(beg, end); //将[beg, end)区间中的数据拷贝赋值给本身。
assign(n, elem); //将n个elem拷贝赋值给本身。
案例: 测试vector的几种赋值操作
#includeusing namespace std; #include void printVector(vector &v) { for (vector ::iterator it = v.begin(); it < v.end(); it++) { cout << *it << " "; } cout << endl; } void test01() { vector v1; for (int i = 0; i < 10; i++) { v1.push_back(i); } printVector(v1); vector v2 = v1; printVector(v2); vector v3; v3.assign(v1.begin(), v1.end()); printVector(v3); vector v4; v4.assign(10, 100); printVector(v4); } int main() { test01(); system("pause"); return 0; }
运行,效果如下
2.4 vector容量和大小
功能描述: 对vector容器的容量和大小操作
函数原型:
empty(); //判断容器是否为空
capacity(); //容器的容量
size(); //返回容器中元素的个数
resize(int num); //重新指定容器的长度为num,若容器变长,则以默认值0填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。
resize(int num, elem); //重新指定容器的长度为num,若容器变长,则以elem值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。
案例: 测试vector容量相关的操作
#includeusing namespace std; #include void printVector(vector &v) { for (vector ::iterator it = v.begin(); it < v.end(); it++) { cout << *it << " "; } cout << endl; } void test01() { vector v1; for (int i = 0; i < 10; i++) { v1.push_back(i); } printVector(v1); if (v1.empty()) { cout << "数组为空" << endl; } else { cout << "数组不为空" << endl; cout << "数组的大小为:" << v1.size() << endl; cout << "数组的容量为:" << v1.capacity() << endl; } v1.resize(15); printVector(v1); cout << "数组的大小为:" << v1.size() << endl; cout << "数组的容量为:" << v1.capacity() << endl; v1.resize(20, 10); printVector(v1); cout << "数组的大小为:" << v1.size() << endl; cout << "数组的容量为:" << v1.capacity() << endl; v1.resize(5); printVector(v1); cout << "数组的大小为:" << v1.size() << endl; cout << "数组的容量为:" << v1.capacity() << endl; } int main() { test01(); system("pause"); return 0; }
运行,效果如下。注意:
①resize改变的是vector的大小,而不是容量。
②如果resize后vector的大小增加,并超过了原来的容量,则会自动增加容量
②如果resize后vector的大小减小,则容量不变。
2.5 vector插入和删除
功能描述: 对vector容器进行插入、删除操作
函数原型:
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(); //删除容器中所有元素
案例: 测试vector的删除和插入操作
#includeusing namespace std; #include void printVector(vector &v) { for (vector ::iterator it = v.begin(); it < v.end(); it++) { cout << *it << " "; } cout << endl; } void test01() { vector v1; for (int i = 0; i < 10; i++) { v1.push_back(i); } printVector(v1); v1.pop_back(); v1.pop_back(); printVector(v1); //首个元素处插入100 v1.insert(v1.begin(), 100); printVector(v1); //首个元素处插入10个100 v1.insert(v1.begin(), 10, 1000); printVector(v1); //删除最后两个元素 v1.erase(v1.end() -2, v1.end()); printVector(v1); //清空数组,不会改变数组的容量 v1.clear(); printVector(v1); cout << "v1的大小为:" << v1.size() << endl; cout << "v1的容量为:" << v1.capacity() << endl; } int main() { test01(); system("pause"); return 0; }
运行,效果如下。
2.6 vector数据存取
功能描述: 对vector中的数据的存取操作
函数原型:
at(int idx); //返回索引idx所指的数据
operator[]; //返回索引idx所指的数据
front(); //返回容器中第一个数据元素
back(); //返回容器中最后一个数据元素
案例: 测试vector的数据存取功能
#includeusing namespace std; #include void printVector(vector &v) { for (vector ::iterator it = v.begin(); it < v.end(); it++) { cout << *it << " "; } cout << endl; } void test01() { vector v1; for (int i = 0; i < 10; i++) { v1.push_back(i); } 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; }
运行,效果如下。
2.7 vector互换容器
功能描述:
实现两个容器内元素进行互换
实现内存收缩
函数原型: swap(vec); // 将vec与本身的元素互换
案例: 测试两个容器内的元素互换的功能、实现内存缩放的功能。
#includeusing namespace std; #include void printVector(vector &v) { for (vector ::iterator it = v.begin(); it < v.end(); it++) { cout << *it << " "; } cout << endl; } void test01() { vector v1; vector v2; for (int i = 0; i < 10; i++) { v1.push_back(i); } printVector(v1); for (int i = 10; i > 0; i--) { v2.push_back(i); } printVector(v2); v1.swap(v2); printVector(v1); printVector(v2); } //实现内存收缩 void test02() { vector 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; //这行代码完成两件事 //1、创建一个和v的size相同的vector匿名对象 //2、将这个匿名对象和v互换(实际上就类似于将两个指针互换) //最后,v的大小和容量就都为3,而匿名对象的大小为3,容量为100000. //这行代码执行完后,匿名对象被释放,所以达到了收缩内存的目的 vector (v).swap(v); cout << "v的容量:" << v.capacity() << endl; cout << "v的大小:" << v.size() << endl; } int main() { test01(); test02(); system("pause"); return 0; }
运行,效果如下。
2.8 vector预留空间
功能描述: 减少vector在动态扩展容量时的扩展次数
函数原型: reserve(int len);//容器预留len个元素长度,预留位置不初始化,元素不可访问。
案例: 通过预留空间和不预留空间,测试在存放大量数据时vector的扩容次数。
#includeusing namespace std; #include void test01() { int num = 0; int* p = NULL; vector v; for (int i = 0; i < 100000; i++) { v.push_back(i); if (p != &v[0]) { p = &v[0]; num++; } } cout << num << endl; } void test02() { int num = 0; int* p = NULL; vector v; v.reserve(100000); for (int i = 0; i < 100000; i++) { v.push_back(i); if (p != &v[0]) { p = &v[0]; num++; } } cout << num << endl; } int main() { test01(); test02(); system("pause"); return 0; }
运行,可以看出预留空间后进行的复制操作大大减少。
三、deque容器 3.1 deque容器基本概念
功能: 双端数组,可以对头端进行插入删除操作
deque与vector区别:
①vector对于头部的插入删除效率低,数据量越大,效率越低
②deque相对而言,对头部的插入删除速度回比vector快
③vector访问元素时的速度会比deque快,这和两者内部实现有关
deque内部工作原理: deque内部有个中控器,维护每段缓冲区中的内容,缓冲区中存放真实数据。中控器维护的是每个缓冲区的地址,使得使用deque时像一片连续的内存空间。
deque容器的迭代器也是支持随机访问的
3.2 deque构造函数
功能描述: deque容器构造
函数原型:
deque; //默认构造形式
deque(beg, end); //构造函数将[beg, end)区间中的元素拷贝给本身。
deque(n, elem); //构造函数将n个elem拷贝给本身。
deque(const deque &deq); //拷贝构造函数
案例: 测试deque的几种构造方式
#includeusing namespace std; #include //为了防止修改deque中元素的值,加上const void printDeque(const deque &d) { //对应的,迭代器也要使用const_iterator for (deque ::const_iterator it = d.begin(); it < d.end(); it++) { cout << *it << " "; } cout << endl; } void test01() { //默认构造 deque d1; for (int i = 0; i < 10; i++) { d1.push_back(i); } printDeque(d1); //将d1的迭代器中间的元素拷贝给d2 deque d2(d1.begin(), d1.end()); printDeque(d2); //将10个100拷贝给d3 deque d3(10, 100); printDeque(d3); //拷贝构造 deque d4(d1); printDeque(d4); } int main() { test01(); system("pause"); return 0; }
运行,效果如下。
3.3 deque赋值操作
功能描述: 给deque容器进行赋值
函数原型:
deque& operator=(const deque &deq); //重载等号操作符
assign(beg, end); //将[beg, end)区间中的数据拷贝赋值给本身。
assign(n, elem); //将n个elem拷贝赋值给本身。
案例: 测试deque的几种赋值操作。
#includeusing namespace std; #include void printDeque(const deque &d) { for (deque ::const_iterator it = d.begin(); it < d.end(); it++) { cout << *it << " "; } cout << endl; } void test01() { //默认构造 deque d1; for (int i = 0; i < 10; i++) { d1.push_back(i); } printDeque(d1); deque d2(d1); printDeque(d2); deque d3(d1.begin(), d1.end()); printDeque(d3); deque d4(10, 100); printDeque(d4); } int main() { test01(); system("pause"); return 0; }
运行,可以看出deque的赋值操作与vector类似,效果如下。
3.4 deque大小操作
功能描述:
①对deque容器的大小进行操作
②deque没有容量的概念。
函数原型:
deque.empty(); //判断容器是否为空
deque.size(); //返回容器中元素的个数
deque.resize(num); //重新指定容器的长度为num,若容器变长,则以默认值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。
deque.resize(num, elem); //重新指定容器的长度为num,若容器变长,则以elem值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。
案例: 测试deque大小相关操作
#includeusing namespace std; #include void printDeque(const deque &d) { for (deque ::const_iterator it = d.begin(); it < d.end(); it++) { cout << *it << " "; } cout << endl; } void test01() { //默认构造 deque 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; } d1.resize(15, 1); cout << "d1的大小为:" << d1.size() << endl; printDeque(d1); d1.resize(5); cout << "d1的大小为:" << d1.size() << endl; printDeque(d1); } int main() { test01(); system("pause"); return 0; }
运行,效果如下。
3.5 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位置的数据,返回下一个数据的位置。
案例: 测试deque的插入和删除操作
#includeusing namespace std; #include void printDeque(const deque &d) { for (deque ::const_iterator it = d.begin(); it < d.end(); it++) { cout << *it << " "; } cout << endl; } //头插法、尾插法 void test01() { cout << "头插法、尾插法:" << endl; deque d1; d1.push_back(1); d1.push_back(2); d1.push_front(100); d1.push_front(200); printDeque(d1); } //插入 void test02() { cout << "插入:" << endl; deque d1; d1.insert(d1.end(), 1); d1.insert(d1.end(), 2); d1.insert(d1.begin(), 100); d1.insert(d1.begin(), 200); printDeque(d1); deque d2; d2.insert(d2.begin(), 3, 100); d2.insert(d2.begin(), d1.begin(), d1.end()); printDeque(d2); } //删除 void test03() { cout << "删除:" << endl; deque d1; d1.insert(d1.end(), 1); d1.insert(d1.end(), 2); d1.insert(d1.end(), 3); d1.insert(d1.begin(), 100); d1.insert(d1.begin(), 200); d1.insert(d1.begin(), 300); printDeque(d1); d1.erase(d1.begin()+2); printDeque(d1); d1.erase(d1.end()-3, d1.end() -1); printDeque(d1); d1.clear(); printDeque(d1); } int main() { test01(); test02(); test03(); system("pause"); return 0; }
运行,效果如下。
3.6 deque数据存取
功能描述: 对deque 中的数据的存取操作
函数原型:
at(int idx); //返回索引idx所指的数据
operator[]; //返回索引idx所指的数据
front(); //返回容器中第一个数据元素
back(); //返回容器中最后一个数据元素
案例: 测试deque的数据存取功能
#includeusing namespace std; #include void printDeque(const deque &d) { for (deque ::const_iterator it = d.begin(); it < d.end(); it++) { cout << *it << " "; } cout << endl; } void test01() { deque d1; d1.push_back(1); d1.push_back(2); d1.push_back(3); d1.push_front(100); d1.push_front(200); d1.push_front(300); printDeque(d1); 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.front() << endl; cout << d1.back() << endl; } int main() { test01(); system("pause"); return 0; }
运行,效果如下。
3.7 deque排序
功能描述: 利用算法实现对deque容器进行排序
算法: sort(iterator beg, iterator end) //对beg和end区间内元素进行排序
案例: 使用sort算法对deque进行排序
#includeusing namespace std; #include #include //需要包含STL算法头文件 void printDeque(const deque &d) { for (deque ::const_iterator it = d.begin(); it < d.end(); it++) { cout << *it << " "; } cout << endl; } void test01() { cout << "排序前:" << endl; deque d1; d1.push_back(1); d1.push_back(2); d1.push_back(3); d1.push_front(100); d1.push_front(200); d1.push_front(300); printDeque(d1); //默认从小到大 //对于支持随机访问的迭代器的容器,都可以利用sort算法直接对其进行排序 //vector也可以利用sort进行排序 sort(d1.begin(), d1.end()); cout << "排序后:" << endl; printDeque(d1); } int main() { test01(); system("pause"); return 0; }
运行,效果如下
四、STL案例----评委打分
题目描述
有5名选手:选手ABCDE,10个评委分别对每一名选手打分,去除最高分,去除评委中最低分,取平均分。
实现步骤
①创建五名选手,放到vector中
②遍历vector容器,取出来每一个选手,执行for循环,可以把10个评分打分存到deque容器中
③sort算法对deque容器中分数排序,去除最高和最低分
④deque容器遍历一遍,累加总分
⑤获取平均分
整体代码
#include#include #include #include #include #include using namespace std; //选手类 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 &v) { string nameSeed = "ABCDE"; int score = 0; for (int i = 0; i < 5; i++) { string name = "选手"; name += nameSeed[i]; Person p(name, score); v.push_back(p); } } //计算平均分 void setScore(vector & v) { for (vector ::iterator it = v.begin(); it != v.end(); it++) { deque deq; int score; for (int i = 0; i < 10; i++) { score = rand() % 41 + 60; deq.push_back(score); } sort(deq.begin(), deq.end()); deq.pop_back(); deq.pop_front(); int sum = 0; for (deque ::iterator it = deq.begin(); it != deq.end(); it++) { sum += *it; } score = sum / deq.size(); (*it).m_Score = score; } } //打印平均分 void showScore(vector &v) { for (vector ::iterator it = v.begin(); it != v.end(); it++) { cout << "姓名:" << it->m_Name << " 平均分:" << it->m_Score << endl; } } int main() { srand((unsigned int)time(NULL)); vector v; createPerson(v); setScore(v); showScore(v); system("pause"); return 0; }
运行,效果如下。



