Detailed introduction to the usage of list in C++

Table of contents

1. List introduction

2. List functions 

       2.1 Member types 

       2.2 Element access

       2.3 Constructor

       2.4list iterator 

       2.5 capacity 

       2.6 Modifier

       2.7 Operation and functions


1. List introduction 

1. List is a sequence container that allows insertion and deletion operations with fixed O(1) time complexity to be performed anywhere in the sequence, and iteration in both directions.
2. The list container is implemented using a double linked list; the double linked list stores each element in a different location, and each node is linked into a sequence list through next and prev pointers.
3. Compared with other standard sequence containers (array, vector and deque), list can usually insert, extract and move elements anywhere within the container.
4. Compared with other standard sequence containers (array, vector and deque), the main disadvantage of list and forward_list (singly linked list implementation) is that they cannot directly access elements through position; for example, to access the fifth element in the list, It must be iterated from a known position (start or end) to that position, requiring linear time overhead.
5. The storage density is low. The list needs to use some additional content space (next, prev) to maintain the link information associated with each element (previous and subsequent linear), resulting in the storage of small element types (such as char, short, int). etc.) have low storage density.
 

2. List functions

2.1 Member types 

 

 

2.2 Element access

 

#include <iostream>
#include <list>

using namespace std;

int main()
{
	list<int> list1 = { 12,23,34 };

	cout << "list1.front():" << list1.front() << endl;
	cout << "list1.back():" << list1.back() << endl;
	
	return 0;
}

2.3 Constructor 

 

#include <iostream>
#include <list>

using namespace std;

template<class T>
void Print(const list<T>& my)
{
	typename list<T>::const_iterator it = my.begin();
	for (; it != my.end(); it++)
	{
		cout << *it << "\t";
	}

	cout << endl;
}

int main()
{
	list<int> list1 = { 12,23,34 };
	list<int> list2(3, 11);
	list<int> list3(list2);

	list<string> list4 = { "This","is","windows" };
	list<string> list5;
	list<string> list6;

	list5 = list4;

	list6.assign(3, "This");

	Print(list1);
	Print(list2);
	Print(list3);
	Print(list4);
	Print(list5);
	Print(list6);

	return 0;
}

2.4list iterator 

 

#include <iostream>
#include <list>

using namespace std;

int main()
{
	list<int> list1 = { 12,23,34 };

	list<int>::const_iterator it1 = list1.begin();
	for (; it1 != list1.end(); it1++)
	{
		cout << *it1 << "\t";
	}

	cout << endl;

	list<int>::reverse_iterator it2 = list1.rbegin() ;
	for (; it2 != list1.rend(); it2++)
	{
		cout << *it2 << "\t";
	}

	cout << endl;
	
	return 0;
}

2.5 capacity 

 

#include <iostream>
#include <list>

using namespace std;

int main()
{
	list<int> list1 = { 12,23,34 };

	cout << "list1.empty():" << list1.empty() << endl;
	cout << "list1.size():" << list1.size() << endl;
	cout << "list1.max_size():" << list1.max_size() << endl;

	return 0;
}

2.6 Modifier 

 

#include <iostream>
#include <list>

using namespace std;

template<class T>
void Print(const list<T>& my)
{
	typename list<T>::const_iterator it = my.begin();
	for (; it != my.end(); it++)
	{
		cout << *it << "\t";
	}

	cout << endl;
}

int main()
{
	list<int> list1 = { 12,23,34 };
	list<int> list2 = { 1,2,3,4,5 };
	
	Print(list1);
	Print(list2);
	auto it = list1.begin();
	list1.insert(it, 55);
	Print(list1);
	it++;
	list1.insert(it, 3, 55);
	Print(list1);

	list1.erase(it);
	Print(list1);

	list1.swap(list2);
	Print(list1);
	Print(list2);


	return 0;
}

2.7 Operation and functions 

 

#include <iostream>
#include <list>

using namespace std;

template<class T>
void Print(const list<T>& my)
{
	typename list<T>::const_iterator it = my.begin();
	for (; it != my.end(); it++)
	{
		cout << *it << "\t";
	}

	cout << endl;
}

int main()
{
	list<int> list1 = { 2,1,9,5,3,7 };
	list<int> list2 = { 1,8,3,6,0,1,5 };
	
	Print(list1);
	Print(list2);

	list1.sort();
	list2.sort();

	Print(list1);
	Print(list2);

	list2.unique();
	Print(list2);

	list1.merge(list2);
	Print(list1);
	Print(list2);

	return 0;
}

 

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_63246738/article/details/131844417