[C++] STL container - list doubly linked list container ③ (Introduction to list commonly used APIs | Insert/delete elements in the middle position | insert function | clear function | erase function)






1. Insert elements into the middle of the list doubly linked list container



1. Insert an element at the specified position - insert function


The function prototype of the std::list#insert function below is to insert a value element at the specified iterator position;

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

After insertion, the element at the original position is squeezed to position + 1;


Code example:

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

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

Complete code example:

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

Results of the :

list container content: 1 2 3 4 5
list container content: 1 2 888 3 4 5
Press any key to continue . . .

Insert image description here


2. Insert n identical elements at the specified position - insert function


The function prototype of the std::list#insert function below is to insert n value elements at the specified iterator position;

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

After insertion, the element at the original position is squeezed to position + n;

Code example:

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

Special note: The index position pointed to by the list<int>::iterator object can only be increased or decreased through the ++ or – operation. You cannot use symbols such as += 5 to jump to multiple index positions at one time;


Complete code example:

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

Results of the :

list container content: 1 2 666 666 666 3 4 5
Press any key to continue . . .

Insert image description here


3. Insert elements within the specified range of another container at the middle position - insert function


Insert elements in the specified range of another container into the middle of the list doubly linked list container;

In the function prototype below, it accepts two iterators first and last, representing an input range;

This function inserts elements in the range [first, last) to the position specified by position;

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

It must be noted that the input iterator range is a front-closed and back-open interval range;


Code example: In the following code, all the contents of the lstInt2 container are inserted at index 2 in the lstInt container;

	// 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());

Results of the :

list container content: 1 2 3 4 5
list container content: 1 2 6 7 8 9 10 3 4 5
Press any key to continue . . .

Insert image description here

In addition, elements from other types of containers can also be inserted. In the following example, all elements in the vector dynamic array are inserted at index position 2 of the list doubly linked list container;

Code example:

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

Results of the :

list container content: 1 2 3 4 5
list container content: 1 2 6 7 8 9 10 3 4 5
Press any key to continue . . .

Insert image description here





2. Delete elements from the middle of list doubly linked list container



1. Delete all elements in the container - clear function


Calling the clear function of the std::list doubly linked list container can delete all elements in the container, and the container becomes an empty doubly linked list;

void clear();

Code example:

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

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

2. Delete the specified element in the container - remove function


Calling the clear function of the std::list doubly linked list container can delete the specified element in the container, match according to the element value, and return the number of deleted elements;

size_type remove(const value_type& value);

Note: If there are multiple elements with the same value as value, multiple elements will be deleted;


Code example: Delete element 3 from the linked list;

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

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

3. Delete the element at the specified iterator position in the container - erase function


Call the erase function of the std::list doubly linked list container and pass in a single iterator pointing to a certain position. positionThe element at position will be deleted and an iterator pointing to the element after the deleted element will be returned;

iterator erase(const_iterator position);

Special note: What is passed in is an iterator pointing to a certain position in the container, not an index value;


Code example: In the following code, delete the element at the specified iterator position in the container;

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

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

4. Delete elements in the specified iterator range in the container - erase function


Call the erase function of the std::list doubly linked list container, pass in iterators pointing to two positions in the container, delete [first, last)all elements in the range, and return an iterator pointing to the element after the deleted element;

iterator erase(const_iterator first, const_iterator last);

Note: The deleted iterator range is a range that is closed at the front and open at the end;


Code Example: The following code removes the last element;

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

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

5. Complete code example - delete element


Complete code example:

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

Results of the :

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

Insert image description here

Guess you like

Origin blog.csdn.net/han1202012/article/details/135212572