C++ list container details

Keywords: C++ Container list

Basic concept of list container

Concept: list is a data structure that stores data in a chain, which is called a linked list

Linked list: a discontinuous storage structure on a physical storage unit. The logical order of data elements is realized through pointer links in the linked list
Composition of linked list: the linked list is composed of a series of nodes
Node composition: it is composed of a data field storing data elements and a pointer field storing the address of the next node

advantage:

  1. You can quickly insert and delete elements anywhere
  2. Dynamic storage allocation will not cause memory waste and overflow

Disadvantages:

  1. Larger footprint than array
  2. The efficiency of accessing elements is low. You need to traverse the linked list to access elements

Iterator:

  1. The storage mode of the linked list is not a continuous memory space, so the iterators in the linked list only support forward and backward, belonging to two-way iterators
  2. Insert and delete operations will not cause the original iterator to fail, which is not true in vector, because the dynamic capacity expansion mechanism of vector will change the storage address of the original data. If the address changes, the iterator will naturally fail

be careful:

  1. The list container in STL is a two-way circular linked list
  2. To use the list container, you need to include the header file #include < list >

1.list constructor

 Function prototype:
 1.list<T> lst;            //The default structure is list, which is implemented by template class
 2.list(begin,end);        //Constructor, using iterators to achieve interval assignment
 3.list(n,elem);           //Constructor to assign n elem s to itself
 4.list(const list &lst);  //copy construction 

In subsequent tests, you often need to print the list, so first define a function to print out

void printList(const list<int>& lst)
{
	cout << "Print list: ";
	for (list<int>::const_iterator it = lst.begin(); it != lst.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

Test case:

void text1()
{
	list<int> lst1;                            //Default construction
	for (int i = 1; i < 10; ++i)
	{
		lst1.push_back(i);
	}
	printList(lst1);

	list<int> lst2(lst1.begin(), lst1.end());  //Constructor for interval assignment
	printList(lst2);

	list<int> lst3(9, 1);                      //Assign 9 1s to itself
	printList(lst3);

	list<int> lst4(lst1);                      //copy construction 
	printList(lst3);
}

Test results:

2. Assignment and exchange of list

 Function prototype:
 1.list& operator=(const list& lst);  //Overload assignment operator
 2.assign(begin,end);                 //Using iterator to realize interval assignment
 3.assign(n,elem);                    //Assign n elem s to itself
 4.swap(lst);                         //Swap lst with itself

Test case:

void text2()
{
	list<int> lst1, lst2, lst3, lst4;
	for (int i = 1; i < 10; ++i)
	{
		lst1.push_back(i);
	}
	printList(lst1);

	lst2 = lst1;                           //Overloaded = assignment
	printList(lst2);

	lst3.assign(lst2.begin(), lst2.end()); //Interval assignment
	printList(lst3);

	lst4.assign(9, 1);                     //Assign 9 1s to itself
	printList(lst4);

	//list exchange
	lst1.swap(lst4);
	cout << "After exchange:" << endl;
	printList(lst1);
	printList(lst4);
}

Test results:

3.list size operation

 Function prototype:
 1.size();            //Returns the number of elements in the container
 2.empty();           //Determine whether the container is empty
 3.resize(num);       //Reassign the length of the container. If it becomes longer, the longer part is assigned a value of 0,
                      //If it becomes shorter, elements exceeding the length of the container are deleted
                      
 4.resize(num,elem);  //Re specify the container length. If it becomes longer, the longer part is assigned elem
                      //If it becomes shorter, elements exceeding the length of the container are deleted

Test case:

void text3()
{
	list<int> lst1;
	for (int i = 1; i < 10; ++i)
	{
		lst1.push_back(i);
	}
	printList(lst1);

	if (!lst1.empty())  //If the container is not empty
	{
		cout << "list The size of the container is:" << lst1.size() << endl;
	}
	
	cout << "Reassign the length to make the container longer: " << endl;
	lst1.resize(15);     //Equivalent to lst1.resize(15,0);
	printList(lst1);

	cout << "Reassign the length to make the container shorter: " << endl;
	lst1.resize(5);     
	printList(lst1);
}

Test results:

4. Insertion and deletion of list

 Function prototype:
 1.push_back(elem);      //Insert an element at the end of the container
 2.push_front(elem);     //Insert an element in the head of the container
 3.pop_back();           //Delete an element at the end of the container
 4.pop_front();          //Delete an element in the container header
 5.insert(pos,elem);     //Insert an element elem at the iterator pos to return the location of the new data
 6.insert(pos,n,elem);   //Insert n element elems at iterator pos with no return value
 7.insert(pos,begin,end);//Insert the data of the [begin,end) interval at the iterator pos, and there is no return value
 8.clear();              //Clear all data in container
 9.erase(begin,end);     //Delete the data in the [begin,end) interval and return the position of the next data
 10.erase(pos);          //Delete the data at the iterator pos and return the location of the next data
 11.remove(elem);        //Delete all elements equal to elem in the container

Test case:

void text4()
{
	list<int> lst1;
	for (int i = 1; i < 6; ++i)
	{
		lst1.push_back(i+5);     //Insert an element at the end of the container
		lst1.push_front(6 - i);  //Insert an element in the head of the container
	}
	printList(lst1);

	lst1.pop_back();        //Delete an element at the end of the container
	lst1.pop_front();       //Delete an element at the end of the container
	printList(lst1);

	list<int> lst2;
	//Insert all data of lst1 in the lst2 header
	lst2.insert(lst2.begin(),lst1.begin(), lst1.end());
	lst2.insert(lst2.end(), 5, 10);  //Insert 5 10s at the end of lst2
	printList(lst2);

	lst2.remove(10);   //Delete the element equal to 10 in lst2
	printList(lst2);
	
	//Delete all elements in lst2
	lst2.erase(lst2.begin(), lst2.end());   //Equivalent to lst2.clear();
	printList(lst2);
}

Test results:

5. Data access of list

 Function prototype:
 1.front();   //Returns the first element of the container
 2.back();    //Returns the last element of the container

Test case:

void text5()
{
	list<int> lst1;
	for (int i = 1; i < 10; ++i)
	{
		lst1.push_back(i);
	}
	printList(lst1);
	cout << "The first element in the container is:" << lst1.front() << endl;
	cout << "The last element in the container is:" << lst1.back() << endl;
}

Test results:

6. Inversion and sorting of list

1.reverse(); //Reverse linked list
2.sort();    //Sort linked list. Note that this is a member function, which will be explained in the test

Test case:

void text6()
{
	list<int> lst1;
	lst1.push_back(5);
	lst1.push_back(1);
	lst1.push_back(7);
	lst1.push_back(3);
	lst1.push_back(6);
	printList(lst1);
	//reversal
	cout << "After reversal:" << endl;
	lst1.reverse();
	printList(lst1);
	//sort
	//sort(lst1.begin(),lst1.end()) / / error
	//All containers that do not support random access iterators cannot use standard algorithms
	//However, containers that do not support random access iterators will provide some algorithms to make up for it
	lst1.sort();  //Sorting algorithm inside list container
	printList(lst1);
}

Test results:

7. Sorting case of list

Case description: sort the user-defined data type of Person. The attributes in Person include name, age and height
Sorting rule: sort by age in ascending order, and sort by height in descending order if the age is equal

Test case:

class Person
{
public:
	string m_name;
	int m_age;
	int m_hight;
	Person(string name, int age, int hight)
	{
		m_name = name;
		m_age = age;
		m_hight = hight;
	}
};
void printList(list<Person>& lst) //Print out the data in the list container
{
	for (list<Person>::iterator it = lst.begin(); it != lst.end(); it++)
	{
		cout << "full name:" << (*it).m_name << "  Age:" << (*it).m_age
			 << "  Height:" << (*it).m_hight << endl;
	}
}
bool comparePerson(Person p1,Person p2)//Collation function
{
	if (p1.m_age == p2.m_age)
	{
		return p1.m_hight > p2.m_hight;
	}
	return p1.m_age < p2.m_age;
}
void text7()
{
	list<Person> lst;
	Person p1("Wang Da", 45, 170);
	Person p2("WangTwo ", 30, 175);
	Person p3("Zhang San", 30, 185);
	Person p4("Li Si", 30, 170);
	Person p5("Wang Wu", 25, 190);
	Person p6("Zhao Liu", 20, 180);
	lst.push_back(p1);
	lst.push_back(p2);
	lst.push_back(p3);
	lst.push_back(p4);
	lst.push_back(p5);
	lst.push_back(p6);
	printList(lst);

	//Sort by rule
	cout << "-------------------------------" << endl;
	cout << "After sorting:" << endl;
	lst.sort(comparePerson);
	printList(lst);
}

Test results:

Posted by enygma on Sat, 30 Oct 2021 15:29:06 -0700