list 双向链表容器 提供了 push_back、pop_back、push_front 和 pop_front 等一系列用于操作列表元素的成员函数 , 函数原型如下 :
void push_front (const value_type& val);
// 头部插入 666
lstInt.push_front(666);
void pop_front ();
// 删除头部元素
lstInt.pop_front();
void push_back (const value_type& val);
// 尾部插入 888
lstInt.push_back(888);
void pop_back ();
// 删除尾部元素
lstInt.pop_back();
上述函数都接受常量引用作为参数( 对于 push_back 和 push_front )或 没有参数(对于 pop_back 和 pop_front) , 并且没有返回值 ;
如果要删除元素 , 确保容器不为空 , 否则会出现 操作未定义 , 程序直接崩溃退出 ;
代码示例 :
#include "iostream"
using namespace std;
#include "list"
// 打印 list 容器内容
void printL(list<int>& lst) {
// 获取迭代器起始位置
list<int>::iterator it = lst.begin();
cout << "list 容器内容 : ";
// 循环判定, 如果没有迭代到最后一个元素的后一个位置, 那么一直循环
while (it != lst.end())
{
// 获取元素值
cout << *it << " ";
// 迭代器指向下一个元素
it++;
}
// 回车换行
cout << endl;
}
int main() {
// list 双向链表容器 使用初始化列表构造
list<int> lstInt{1, 2, 3, 4, 5};
// 打印 list 容器内容
printL(lstInt);
// 头部插入 666
lstInt.push_front(666);
// 尾部插入 888
lstInt.push_back(888);
// 打印 list 容器内容
printL(lstInt);
// 删除头部元素
lstInt.pop_front();
// 删除尾部元素
lstInt.pop_back();
// 打印 list 容器内容
printL(lstInt);
// 控制台暂停 , 按任意键继续向后执行
system("pause");
return 0;
};
执行结果 :
list 容器内容 : 1 2 3 4 5
list 容器内容 : 666 1 2 3 4 5 888
list 容器内容 : 1 2 3 4 5
请按任意键继续. . .
std::list 是一个双向链表容器 提供了 back 和 front 这两个成员函数 , 用于访问链表的最后一个和第一个元素 , 函数原型如下 :
reference front();
const_reference front() const;
reference back();
const_reference back() const;
代码示例 :
// list 双向链表容器 使用初始化列表构造
list<int> lstInt{1, 2, 3, 4, 5};
cout << "首元素 : " << lstInt.front() << endl;
cout << "尾元素 : " << lstInt.back() << endl;
完整代码示例 :
#include "iostream"
using namespace std;
#include "list"
// 打印 list 容器内容
void printL(list<int>& lst) {
// 获取迭代器起始位置
list<int>::iterator it = lst.begin();
cout << "list 容器内容 : ";
// 循环判定, 如果没有迭代到最后一个元素的后一个位置, 那么一直循环
while (it != lst.end())
{
// 获取元素值
cout << *it << " ";
// 迭代器指向下一个元素
it++;
}
// 回车换行
cout << endl;
}
int main() {
// list 双向链表容器 使用初始化列表构造
list<int> lstInt{1, 2, 3, 4, 5};
// 打印完整列表
printL(lstInt);
cout << "首元素 : " << lstInt.front() << endl;
cout << "尾元素 : " << lstInt.back() << endl;
// 控制台暂停 , 按任意键继续向后执行
system("pause");
return 0;
};
执行结果 :
list 容器内容 : 1 2 3 4 5
首元素 : 1
尾元素 : 5
请按任意键继续. . .
std::list 双向链表容器 提供了 begin、end、rbegin 和 rend 这几个成员函数,用于 获取 迭代访问链表中的元素 的 迭代器 , 函数原型如下 :
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
reverse_iterator rend();
const_reverse_iterator rend() const;
迭代器的位置如下图所示 :
// 正向迭代
for (list<int>::iterator it = lstInt.begin(); it != lstInt.end(); it++)
{
cout << *it << " ";
}
cout << endl;
// 反向迭代
for (list<int>::reverse_iterator rit = lstInt.rbegin(); rit != lstInt.rend(); rit++)
{
cout << *rit << " ";
}
cout << endl;
代码示例 :
#include "iostream"
using namespace std;
#include "list"
// 打印 list 容器内容
void printL(list<int>& lst) {
// 获取迭代器起始位置
list<int>::iterator it = lst.begin();
cout << "list 容器内容 : ";
// 循环判定, 如果没有迭代到最后一个元素的后一个位置, 那么一直循环
while (it != lst.end())
{
// 获取元素值
cout << *it << " ";
// 迭代器指向下一个元素
it++;
}
// 回车换行
cout << endl;
}
int main() {
// list 双向链表容器 使用初始化列表构造
list<int> lstInt{1, 2, 3, 4, 5};
// 正向迭代
for (list<int>::iterator it = lstInt.begin(); it != lstInt.end(); it++)
{
cout << *it << " ";
}
cout << endl;
// 反向迭代
for (list<int>::reverse_iterator rit = lstInt.rbegin(); rit != lstInt.rend(); rit++)
{
cout << *rit << " ";
}
cout << endl;
// 控制台暂停 , 按任意键继续向后执行
system("pause");
return 0;
};
执行结果 :
1 2 3 4 5
5 4 3 2 1
请按任意键继续. . .