【C++】STL 容器 - list 双向链表容器 ③ ( list 常用 api 简介 | 中间位置 插入 / 删除 元素 | insert 函数 | clear 函数 | erase 函数 )






一、list 双向链表容器 的 中间位置 插入 元素



1、在指定位置插入 1 个元素 - insert 函数


下面的 std::list#insert 函数原型的作用是 在 指定的 迭代器位置 position 上 , 插入 1 个 value 值元素 ;

iterator insert(const_iterator position, const value_type& value);

插入后 , 原来 position 位置的元素 , 被挤到 position + 1 位置上 ;


代码示例 :

	// 获取指向首元素的迭代器, 当前指向索引 0
	list<int>::iterator it = lstInt.begin();
	// 执行后指向索引 1
	it++;
	// 执行后指向索引 2
	it++;

	// 在索引 2 位置插入 1 个 888 元素
	lstInt.insert(it, 888);

完整代码示例 :

#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);

	// 获取指向首元素的迭代器, 当前指向索引 0
	list<int>::iterator it = lstInt.begin();
	// 执行后指向索引 1
	it++;
	// 执行后指向索引 2
	it++;

	// 在索引 2 位置插入 1 个 888 元素
	lstInt.insert(it, 888);

	// 打印 list 双向链表容器
	printL(lstInt);


	// 控制台暂停 , 按任意键继续向后执行
	system("pause");

	return 0;
};

执行结果 :

list 容器内容 : 1 2 3 4 5
list 容器内容 : 1 2 888 3 4 5
Press any key to continue . . .

在这里插入图片描述


2、在指定位置插入 n 个相同元素 - insert 函数


下面的 std::list#insert 函数原型的作用是 在 指定的 迭代器位置 position 上 , 插入 n 个 value 值元素 ;

void insert(const_iterator position, size_type n, const value_type& value);  

插入后 , 原来 position 位置的元素 , 被挤到 position + n 位置上 ;

代码示例 :

	// list 双向链表容器 使用初始化列表构造
	list<int> lstInt{
    
     1, 2, 3, 4, 5 };

	// 获取指向首元素的迭代器, 当前指向索引 0
	list<int>::iterator it = lstInt.begin();
	// 执行后指向索引 1
	it++;
	// 执行后指向索引 2
	it++;

	// 在索引 2 位置插入 3 个 666 元素
	lstInt.insert(it, 3, 666);

特别注意 : list<int>::iterator 对象指向的索引位置 , 只能通过 ++ 或 – 运算进行增减 , 不能使用 += 5 等符号一次性跳转多个索引位置 ;


完整代码示例 :

#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 };

	// 获取指向首元素的迭代器, 当前指向索引 0
	list<int>::iterator it = lstInt.begin();
	// 执行后指向索引 1
	it++;
	// 执行后指向索引 2
	it++;

	// 在索引 2 位置插入 3 个 666 元素
	lstInt.insert(it, 3, 666);

	// 打印 list 双向链表容器
	printL(lstInt);


	// 控制台暂停 , 按任意键继续向后执行
	system("pause");

	return 0;
};

执行结果 :

list 容器内容 : 1 2 666 666 666 3 4 5
Press any key to continue . . .

在这里插入图片描述


3、中间位置 插入 另一个容器的指定范围内的 元素 - insert 函数


在 list 双向链表容器 的 中间位置 插入 另一个容器的指定范围内的 元素 ;

下面的函数原型中 , 接受两个迭代器 first 和 last , 表示一个输入范围 ;

该函数将范围 [first, last) 中的元素插入到 position 指定的位置 ;

template <class InputIt>  
void insert(const_iterator position, InputIt first, InputIt last);

一定要注意 , 输入的 迭代器范围是一个 前闭后开 区间范围 ;


代码示例 : 下面的代码中 , 在 lstInt 容器中的 索引 2 位置 , 插入了 lstInt2 容器中的所有内容 ;

	// list 双向链表容器 使用初始化列表构造
	list<int> lstInt{
    
     1, 2, 3, 4, 5 };
	list<int> lstInt2{
    
     6, 7, 8, 9, 10 };

	// 获取指向首元素的迭代器, 当前指向索引 0
	list<int>::iterator it = lstInt.begin();
	// 执行后指向索引 1
	it++;
	// 执行后指向索引 2
	it++;

	// 在索引 2 位置插入 lstInt2 中的所有元素
	lstInt.insert(it, lstInt2.begin(), lstInt2.end());

执行结果 :

list 容器内容 : 1 2 3 4 5
list 容器内容 : 1 2 6 7 8 9 10 3 4 5
Press any key to continue . . .

在这里插入图片描述

此外 , 还可以插入 其它类型 容器的元素 , 下面的示例中 , 在 list 双向链表容器 的 2 号索引位置 , 插入了 vector 动态数组 中的所有元素 ;

代码示例 :

#include "iostream"
using namespace std;
#include "list"
#include "vector"

// 打印 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 };
	vector<int> vec{
    
     6, 7, 8, 9, 10 };

	// 打印 list 双向链表容器
	printL(lstInt);

	// 获取指向首元素的迭代器, 当前指向索引 0
	list<int>::iterator it = lstInt.begin();
	// 执行后指向索引 1
	it++;
	// 执行后指向索引 2
	it++;

	// 在索引 2 位置插入 lstInt2 中的所有元素
	lstInt.insert(it, vec.begin(), vec.end());

	// 打印 list 双向链表容器
	printL(lstInt);


	// 控制台暂停 , 按任意键继续向后执行
	system("pause");

	return 0;
};

执行结果 :

list 容器内容 : 1 2 3 4 5
list 容器内容 : 1 2 6 7 8 9 10 3 4 5
Press any key to continue . . .

在这里插入图片描述





二、list 双向链表容器 的 中间位置 删除 元素



1、删除容器中所有元素 - clear 函数


调用 std::list 双向链表容器 的 clear 函数 , 可以删除 容器中的所有元素 , 容器变成了一个空的 双向链表 ;

void clear();

代码示例 :

	// list 双向链表容器 使用初始化列表构造
	list<int> lstInt{
    
     1, 2, 3, 4, 5 };

	// 删除容器中的所有元素
	lstInt.clear();

2、删除容器中指定元素 - remove 函数


调用 std::list 双向链表容器 的 clear 函数 , 可以删除 容器中的 指定元素 , 根据 元素值 进行匹配 , 返回 被删除的元素个数 ;

size_type remove(const value_type& value);

注意 : 如果有多个 元素 与 value 值相同 , 那么会删除多个元素 ;


代码示例 : 删除链表中的 元素 3 ;

	// list 双向链表容器 使用初始化列表构造
	list<int> lstInt{
    
     1, 2, 3, 4, 5 };

	// 删除容器中的指定元素
	lstInt.remove(3);

3、删除容器中指定 迭代器位置 的元素 - erase 函数


调用 std::list 双向链表容器 的 erase 函数 , 传入 单个 指向某 position 位置的迭代器 , 则会删除位于 position 位置的元素 , 并返回一个指向被删除元素之后元素的迭代器 ;

iterator erase(const_iterator position);

特别注意 : 传入的是 指向容器中某个位置的 迭代器 , 不是索引值 ;


代码示例 : 在下面的代码中 , 删除容器中的指定迭代器位置的元素 ;

	// list 双向链表容器 使用初始化列表构造
	list<int> lstInt{
    
     1, 2, 3, 4, 5 };

	// 删除容器中的指定迭代器位置的元素
	lstInt.erase(lstInt.begin());

4、删除容器中指定 迭代器范围 的元素 - erase 函数


调用 std::list 双向链表容器 的 erase 函数 , 传入 指向容器两个位置的 迭代器 , 删除位于范围 [first, last) 中的所有元素 , 并返回一个指向被删除元素之后元素的迭代器 ;

iterator erase(const_iterator first, const_iterator last);

注意 : 删除的 迭代器范围 是一个 前闭后开 区间 的范围 ;


代码示例 : 下面的代码 删除了 最后一个元素 ;

	// list 双向链表容器 使用初始化列表构造
	list<int> lstInt{
    
     1, 2, 3, 4, 5 };

	// 删除容器中的指定迭代器范围的元素
	lstInt.erase(--lstInt.end(), lstInt.end());

5、完整代码示例 - 删除元素


完整代码示例 :

#include "iostream"
using namespace std;
#include "list"
#include "vector"

// 打印 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);

	// 删除容器中的指定迭代器位置的元素
	lstInt.erase(lstInt.begin());
	// 打印 list 双向链表容器
	printL(lstInt);

	// 删除容器中的指定迭代器范围的元素
	lstInt.erase(--lstInt.end(), lstInt.end());
	// 打印 list 双向链表容器
	printL(lstInt);

	// 删除容器中的指定元素
	lstInt.remove(3);
	// 打印 list 双向链表容器
	printL(lstInt);

	// 删除容器中的所有元素
	lstInt.clear();
	// 打印 list 双向链表容器
	printL(lstInt);


	// 控制台暂停 , 按任意键继续向后执行
	system("pause");

	return 0;
};

执行结果 :

list 容器内容 : 1 2 3 4 5
list 容器内容 : 2 3 4 5
list 容器内容 : 2 3 4
list 容器内容 : 2 4
list 容器内容 :
Press any key to continue . . .

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/han1202012/article/details/135212572