C++ notes list use

List is a doubly linked list, header file #include<list>

1. Basic operation

insert:Headend, tailend, middle (through iterators)
Find:There is no member function, only the iterator can be returned by find(ilist.begin(), ilist.end(), 1)
delete:Headend, tailend, middle (through iterators)
Sort:Member function ilist.sort(), default ascending
reverse:Ilist.reverse(), element reversal
Empty:Ilist.empty(), if empty, returns 1; if not empty, returns 0.
Empty:ilist.clear(). 

2. Function list

Ilist.assign() assigns a value to the list

Ilist.begin() returns an iterator pointing to the first element

Ilist.end() returns the end of the iterator

Ilist.rbegin() returns a reverse iterator pointing to the first element

Ilist.rend() points to the reverse iterator at the end of the list

Ilist.clear() removes all elements

Ilist.empty() returns true if the list is empty

Ilist.push_back() adds an element to the end of the list

Ilist.push_front() adds an element to the head of the list

Ilist.front() returns the first element

Ilist.pop_front() removes the first element

Ilist.back() returns the last element

Ilist.pop_back() removes the last element

Ilist.remove() removes an element from the list

Ilist.remove_if() deletes elements by specified criteria

Ilist.get_allocator() returns the configurator for list

Ilist.insert() inserts an element into the list

Ilist.max_size() returns the maximum number of elements a list can hold

Ilist.merge() merges two lists

Ilist.resize() changes the size of the list

Ilist.size() returns the number of elements in the list

Ilist.sort() sorts list

Ilist.reverse() reverses the elements of list

Ilist.splice() merges two lists

Ilist.swap() exchanges two lists

Ilist.unique() removes duplicate elements from the list

3. Program example

#include<iostream>
#include<list>
#include<algorithm> 
using namespace std;

int main(){
	
	list<int> ilist;
	list<int>::iterator it;
	 
	//insert
	 Ilist.push_back(1);//tail insertion 
	ilist.push_back(2);
	ilist.push_back(6);
	ilist.push_back(9);
	 Ilist.push_front(3);//Head insertion
	ilist.push_front(4);
	ilist.push_front(7);
	ilist.push_front(8);
	 // lookup 
	it=find(ilist.begin(),ilist.end(),1);
	 It=ilist.insert(it,5);//Intermediate insertion
	 Cout<<"it position:"<<*it<<endl;
	
	 //output
	 Cout<<"list size:"<<ilist.size()<<endl; 
	for(it=ilist.begin();it!=ilist.end();it++) cout<<*it<<' ';
	cout<<endl<<endl;
	
	
	 //delete
	 Cout<<" delete header element: "<<ilist.front()<<endl;//return header element 
	 Ilist.pop_front(); //delete header element 
	 Cout<<" removes the tail element: "<<ilist.back()<<endl;//returns the tail element 
	 Ilist.pop_back(); //Delete tail element 
	it=find(ilist.begin(),ilist.end(),3);	
	 Cout<<" deletes the middle element: "<<*it<<endl;
	 Ilist.erase(it); //delete the middle 4 
	 Cout<<" delete intermediate element: 1"<<endl; 
	 Ilist.remove(1); // directly delete the element 
	
	 Cout<<" after deleting the list size: "<<ilist.size()<<endl; 
	for(it=ilist.begin();it!=ilist.end();it++) cout<<*it<<' ';
	cout<<endl<<endl;
	
	 //sort
	 Ilist.sort();//Default ascending 
	 Cout<<" default sort: "; 
	for(it=ilist.begin();it!=ilist.end();it++) cout<<*it<<' ';
	cout<<endl;
	 Cout<<"reverse output:";
	 Ilist.reverse();//Element reverse 
	for(it=ilist.begin();it!=ilist.end();it++) cout<<*it<<' ';
	cout<<endl<<endl;	
	/*
	list<int> ilist2(ilist.rbegin(),ilist.rend());
	for(it=ilist2.begin();it!=ilist2.end();it++) cout<<*it<<' ';		
	*/
	
	 / / Empty, empty
	 Is cout<<"list empty:"<<ilist.empty()<<endl;
	ilist.clear();
	 Is cout<<"list empty:"<<ilist.empty()<<endl;	 
	
	return 0;
} 

operation result:


reference

Intelligent Recommendation

Use the list of C language

C language is difficult to say that the list beginner, come to you today to talk about the list Speaking before the list had to tell us about the structure, before we learn the list should have been s...

c # in use exemplary list

Reproduced in: https: //my.oschina.net/smartsmile/blog/815512...

c++ std::list use

One requirement is that there are five boxes and put the items in the smallest box FR: hunk Xu QQ technical exchange group: 386476712...

[C++] Introduction and use of list

The following is Ali’s summary of the list container. I hope it will be helpful to everyone. If there is any mistake, please point out 1: Introduction to list 2: Use of list 1: Introduction to l...

More Recommendation

The creation and use of List in C#

Collection class List When we have many data of the same type, we can use arrays for storage and management, but the disadvantage of this is that the size of the array is predetermined and fixed. If w...

C# List use pitfalls

Basic knowledge is not solid, use List<T>, unexpected results appear It should output as expected: 2 3 4 5 The actual result is: 5 5 5 5 It turns out that my basic knowledge is not solid enough ...

(C#) List use

The basics and common methods of List:   Common function calls in List A. Add elements:   B. Insert element: Insert(int index, T item); Add an element at the index position   c. delete ...

[C++] How to use list

List is a serial container. The function performed by the list container is actually very similar to the doubly linked list in the data structure. The data elements in the list are connected to a line...

Use of C# List

The creation and use of List Create a list (a list can store any type of data. When creating a list object, you must first specify what type of list you want to create) (generic) Insert data Read data...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top