List in C++ | Set 2 (Some Useful Functions)

We have discussed List and some of its functions in the following article:

List : Sequence Containers

Some more useful functions are discussed in this article

1. emplace(position, value) :- This function is used to insert an element at the position specified. 在特定位置插入值

2. emplace_back(value) :- This function adds value at end of list. It is different from push_back() by the fact that it directly creates element at position whereas push_back() first makes a temporary copy and copies from there. emplace_back() is faster in implementation than push_back() in most situations. 函数在list的最后插入值,和push_back不同,它直接在那个位置创建元素,而push_back首先创建临时的副本,然后复制到那里,大多情况下emplace_back比push_back快。

3. emplace_front :- This function adds value at beginning of list. It is different from push_front() by the fact that it directly creates element at position whereas push_front() first makes a temporary copy and copies from there. emplace_front() is faster in implementation than push_front() in most situations. 在list的前面插入值,和push_front不同,直接在那个位置创建值,而push_front是创建临时的副本,然后复制到那里,emplace_front比push_front快。

// C++ code to demonstrate the working of 
// emplace(), emplace_front() and emplace_back() 
#include<iostream> 
#include<list> // for list functions 
using namespace std; 
int main() 
{ 
	// Declaring a list 
	list<int> gqlist; 
	
	// Initialising list iterator 
	list<int>::iterator it= gqlist.begin(); 
	
	// Entering list element using emplace_back() 
	for (int i=1; i<=5; i++) 
	gqlist.emplace_back(i); 
	
	// Displaying list elements 
	cout << "List after emplace_back operation is : "; 
	for (int &x : gqlist) cout << x << " "; 
	cout << endl; 
	
	// Entering list element using emplace_front() 
	for (int i=10; i<=50; i+=10) 
	gqlist.emplace_front(i); 
	
	// Displaying list elements 
	cout << "List after emplace_front operation is : "; 
	for (int &x : gqlist) cout << x << " "; 
	cout << endl; 
	
	// using advance() to advance iterator position 
	advance(it,2); 
	
	// inserting element at 2nd position using emplace() 
	gqlist.emplace(it,100); 
	
	// Displaying list elements 
	cout << "List after emplace operation is : "; 
	for (int &x : gqlist) cout << x << " "; 
	cout << endl; 
	
	return 0; 
	
} 

Output:

List after emplace_back operation is : 1 2 3 4 5 
List after emplace_front operation is : 50 40 30 20 10 1 2 3 4 5 
List after emplace operation is : 50 100 40 30 20 10 1 2 3 4 5 

4. merge(list2) :- This function is used to merge list2 with list1. If both the lists are in sorted order, then the resulting list is also sorted.把list1和list2融合到一起,如果两个list都是有序的,那么结果也是有序的。

5. remove_if(condition) :- This function removes the element from list on the basis of condition given in its argument. 如果条件符合,则删除符合条件的元素。

// C++ code to demonstrate the working of 
// merge() and remove_if() 
#include<iostream> 
#include<list> // for list functions 
using namespace std; 
int main() 
{ 
	// Initializing list1 
	list<int> gqlist1 = {1, 2, 3}; 
	
	// Initializing list2 
	list<int> gqlist2 = {2, 4, 6}; 
	
	// using merge() to merge list1 with list2 
	gqlist1.merge(gqlist2); 
	
	// Displaying list elements 
	cout << "list1 after merge operation is : "; 
	for (int &x : gqlist1) cout << x << " "; 
	cout << endl; 
	
	// using remove_if() to remove odd elements 
	// removes 1 and 3 
	gqlist1.remove_if([](int x){return x%2!=0;}); 
	
	// Displaying list elements 
	cout << "list1 after remove_if operation is : "; 
	for (int &x : gqlist1) cout << x << " "; 
	cout << endl; 
	
	return 0; 
	
} 

Output:

list1 after merge operation is : 1 2 2 3 4 6 
list1 after remove_if operation is : 2 2 4 6 

6. unique() :- This function is used to delete the repeated occurrences of the number. List has to be sorted for this function to get executed. 删除重复出现的元素,list必须是有序的。

7. splice(position, list2) :- This function is used to transfer elements from one list into another.把一个list中的元素传送到另一个list。

// C++ code to demonstrate the working of 
// unique() and splice() 
#include<iostream> 
#include<list> // for list functions 
using namespace std; 
int main() 
{ 
	// Initializing list1 
	list<int> gqlist1 = {1, 1, 1, 2, 2, 3, 3, 4}; 
	
	// Initializing list2 
	list<int> gqlist2 = {2, 4, 6}; 
	
	// Initializing list1 iterator 
	list<int>::iterator it = gqlist1.begin(); 
	
	// using advance() to increment iterator position 
	advance(it, 3); 
	
	
	// Displaying list elements 
	cout << "list1 before unique operation is : "; 
	for (int &x : gqlist1) cout << x << " "; 
	cout << endl; 
	
	// using unique() to remove repeating elements 
	gqlist1.unique(); 
	
	// Displaying list elements 
	cout << "list1 after unique operation is : "; 
	for (int &x : gqlist1) cout << x << " "; 
	cout << endl << endl; 
	
	// using splice() to splice list2 in list1 at position it 
	// inserts list2 after 2nd position 
	gqlist1.splice(it, gqlist2); 
	
	// Displaying list elements 
	cout << "list1 after splice operation is : "; 
	for (int &x : gqlist1) cout << x << " "; 
	cout << endl; 
	
	
	return 0; 
	
} 

Output:

list1 before unique operation is : 1 1 1 2 2 3 3 4 
list1 after unique operation is : 1 2 3 4 

list1 after splice operation is : 1 2 4 6 2 3 4 

8. swap(list2) :- This function is used to swap one list element with other. 交换两个list的元素。

// C++ code to demonstrate the working of 
// swap() 
#include<iostream> 
#include<list> // for list functions 
using namespace std; 
int main() 
{ 
	// Initializing list1 
	list<int> gqlist1 = {1, 2, 3, 4}; 
	
	// Initializing list1 
	list<int> gqlist2 = {2, 4, 6}; 
	
	// Displaying list before swapping 
	cout << "The contents of 1st list "
			"before swapping are : "; 
	for (int &x : gqlist1) 
		cout << x << " "; 
	cout << endl; 
	cout << "The contents of 2nd list "
			"before swapping are : "; 
	for (int &x : gqlist2) 
		cout << x << " "; 
	cout << endl; 

	// Use of swap() to swap the list 
	gqlist1.swap(gqlist2); 

	// Displaying list after swapping 
	cout << "The contents of 1st list "
			"after swapping are : "; 
	for (int &x : gqlist1) 
		cout << x << " "; 
	cout << endl; 

	cout << "The contents of 2nd list "
			"after swapping are : "; 
	for (int &x : gqlist2) 
		cout << x << " "; 
	cout << endl; 
	
	return 0; 
	
} 

Output:

The contents of 1st list before swapping are : 1 2 3 4 
The contents of 2nd list before swapping are : 2 4 6 
The contents of 1st list after swapping are : 2 4 6 
The contents of 2nd list after swapping are : 1 2 3 4 

This article is contributed by Manjeet Singh .If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

猜你喜欢

转载自blog.csdn.net/qq_27009517/article/details/86524487