文章目录
stack容器
stack基本概念
- stack是一种先进后出的数据结构,它只有一个出口
- 栈中只有顶端元素才可以被外界使用,因此栈不支持遍历操作
stack常用接口
stack代码示例
#include <iostream>
#include <stack>
using namespace std;
void test() {
// 默认构造函数
stack<int> s1;
// 入栈
s1.push(1);
s1.push(2);
s1.push(3);
// 出栈
s1.pop();
// 拷贝构造
stack<int> s2(s1);
// 赋值操作
stack<int> s3;
s3 = s2;
cout << "判断是否为空:" << s1.empty() << endl;
cout << "返回栈的大小:" << s1.size() << endl;
// 获取栈顶
cout << "获取栈顶:" << s1.top() << endl;
}
int main() {
test();
return 0;
}
运行结果
queue 容器
queue基本概念
- queue是一种先进先出的数据结构,他有两个出口
-
队列容器允许从一端新增元素,从另一端移除元素
-
队列中只有队头和队尾才能被外界使用,因此队列不允许有遍历操作
queue常用操作
代码示例
#include <iostream>
#include <queue>
using namespace std;
void test() {
// 默认构造函数
queue<int> q1;
// 向队尾添加元素
q1.push(1);
q1.push(2);
q1.push(3);
q1.push(4);
q1.push(5);
q1.push(6);
// 拷贝构造函数
queue<int> q2(q1);
// 出队
q1.pop();
// 赋值操作
queue<int> q3 = q1;
cout << "返回队头元素:" << q1.front() << endl;
cout << "返回队尾元素:" << q1.back() << endl;
cout << "判断队列是否为空:" << q1.empty() << endl;
cout << "返回队列大小:" << q1.size() << endl;
}
int main() {
test();
return 0;
}
运行结果
list容器
list基本概念
-
功能:将数据进行链式存储
-
链表:是一种物理存储上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链接实现的
-
链表的组成:链表是由一系列节点组成的
-
节点的组成:一个存储数据元素的数据域,另一个是存储下一个节点地址的指针域
-
STL中链表是一个双向循环链表
-
双向:每个节点即指向前一个节点也指向后一个节点
-
循环:头尾节点也是通过指针相连的
-
优缺点
优点
-
可以对任意一个位置进行快速的插入和删除
-
随用随开不会造成内存的浪费和溢出(数组存储会存在多开辟的情况)
-
链表插入和删除操作都不会造成原有list迭代器的失效,在vector中是不成立的
缺点
-
容器遍历速度没有数组快
-
占用空间比数组大(除了数据还有指针)
-
由于链表的存储方式不是一个连续的存储空间,因此链表list中的迭代器只支持前移和后裔操作,不支持随机访问,属于双向迭代器
构造函数
功能描述
- 创建list容器
函数原型
代码示例
#include <iostream>
#include <list>
using namespace std;
void printList(const list<int> l) {
for (list<int>::const_iterator it = l.begin(); it != l.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
void test() {
// 默认构造函数
list<int> l1;
for (int i = 0; i < 5; i++) {
l1.push_back(i);
}
printList(l1);
// 拷贝构造
list<int> l2(l1);
printList(l2);
// 使用范围构造
list<int> l3(l1.begin(), l1.end());
printList(l3);
// 使用n个x值构造
list<int> l4(3, 10);
printList(l4);
}
int main() {
test();
return 0;
}
运行结果
list赋值和交换
函数原型
代码示例
#include <iostream>
#include <list>
using namespace std;
void printList(const list<int> l) {
for (list<int>::const_iterator it = l.begin(); it != l.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
void test() {
// 默认构造函数
list<int> l1;
for (int i = 0; i < 5; i++) {
l1.push_back(i);
}
printList(l1);
cout << "使用operator=进行赋值" << endl;
list<int> l2 = l1;
printList(l2);
cout << "使用assign 按照指定区间赋值" << endl;
list<int> l3;
l3.assign(l1.begin(), l1.end());
printList(l3);
cout << "使用assign赋值n个value值" << endl;
list<int> l4;
l4.assign(4, 10);
printList(l4);
cout << "交换l1和l4" << endl;
l1.swap(l4);
printList(l1);
printList(l4);
}
int main() {
test();
return 0;
}
运行结果
list容器大小操作
函数原型
代码示例
#include <iostream>
#include <list>
using namespace std;
void printList(const list<int> l) {
for (list<int>::const_iterator it = l.begin(); it != l.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
void test() {
// 默认构造函数
list<int> l1;
if (l1.empty()) {
cout << "容器为空" << endl;
} else {
cout << "容器不为空" << endl;
}
for (int i = 0; i < 5; i++) {
l1.push_back(i);
}
printList(l1);
cout << "元素个数为:" << l1.size() << endl;
l1.resize(3);
printList(l1);
l1.resize(5);
printList(l1);
l1.resize(10, 2);
printList(l1);
}
int main() {
test();
return 0;
}
运行结果
list容器插入和删除
函数原型
代码示例
#include <iostream>
#include <list>
using namespace std;
void printList(const list<int> l) {
for (list<int>::const_iterator it = l.begin(); it != l.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
void test() {
// 默认构造函数
list<int> l1;
cout << "尾插: " << endl;
l1.push_back(1);
l1.push_back(2);
l1.push_back(3);
printList(l1);
cout << "头插: " << endl;
l1.push_front(4);
l1.push_front(5);
l1.push_front(6);
printList(l1);
cout << "尾删: " << endl;
l1.pop_back();
printList(l1);
cout << "头删: " << endl;
l1.pop_front();
printList(l1);
cout << "指定位置插入: " << endl;
l1.insert(l1.begin(), 3);
printList(l1);
cout << "删除指定位置的元素: " << endl;
list<int>::iterator it = l1.begin();
l1.erase(++it);
printList(l1);
cout << "clear清空容器" << endl;
l1.clear();
printList(l1);
l1.push_front(1);
l1.push_front(1);
l1.push_front(2);
l1.push_front(1);
cout << "remove删除容器中所有与给定值相等的元素" << endl;
l1.remove(1);
printList(l1);
}
int main() {
test();
return 0;
}
运行结果
list存取
功能描述
- 对list容器中数据进行存取
函数原型
代码示例
#include <iostream>
#include <list>
using namespace std;
void printList(const list<int> l) {
for (list<int>::const_iterator it = l.begin(); it != l.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
void test() {
// 默认构造函数
list<int> l1;
for (int i = 0; i < 5; i++) {
l1.push_back(i);
}
cout << "容器初始元素: ";
printList(l1);
cout << "返回第一个元素" << l1.front() << endl;
cout << "返回最后一个元素" << l1.back() << endl;
}
int main() {
test();
return 0;
}
运行结果
list容器反转和排序
功能描述
- 将容器中的元素反转,以及将容器中的元素进行排序
函数原型
代码示例
#include <iostream>
#include <list>
using namespace std;
void printList(const list<int> l) {
for (list<int>::const_iterator it = l.begin(); it != l.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
bool myCompare(int a, int b) {
return a > b;
}
void test() {
// 默认构造函数
list<int> l1;
l1.push_back(4);
l1.push_back(7);
l1.push_back(1);
l1.push_back(3);
l1.push_back(2);
l1.push_back(8);
l1.push_back(5);
cout << "容器初始元素: ";
printList(l1);
cout << "反转后元素: ";
l1.reverse();
printList(l1);
cout << "排序后的元素(默认升序排列): ";
l1.sort();
printList(l1);
cout << "使用自定义比较器降序排列: ";
l1.sort(myCompare);
printList(l1);
}
int main() {
test();
return 0;
}