C++ list linked list learning

list container (linked list)

list constructor

Function prototype:

  • list list; //list is implemented by template class, the default construction form of the object
  • list(beg,end); //The constructor copies the elements in the range (beg,end) to itself
  • list(n,elem); //The constructor copies n elems to itself
  • list(const list &list); //Example of copy constructor
    :
#include<iostream>
#include <list>
using namespace std;

//list容器构造函数
//-list<T> list;    //list采用模板类实现,对象的默认构造形式
//-list(beg, end);    //构造函数将(beg,end)区间中的元素拷贝给本身
//-list(n, elem);    //构造函数将n个elem拷贝给本身
//-list(const list & list);    //拷贝构造函数

void PrintList(list<int> &l)
{
    
    
	for (list<int>::iterator it = l.begin(); it != l.end(); it++)
	{
    
    
		cout << *it << "  ";
	}
	cout << endl;
}

void PrintList(list<char>& l)
{
    
    
	for (list<char>::iterator it = l.begin(); it != l.end(); it++)
	{
    
    
		cout << *it << "  ";
	}
	cout << endl;
}

void _01Test01()
{
    
    
	list<int> list1;
	for (int i = 0; i < 10; i++)
	{
    
    
		list1.push_back(i);
	}
	cout << "list1: ";
	PrintList(list1);


	list<int> list2(list1.begin(), list1.end());
	cout << "list2: ";
	PrintList(list2);

	list<char>  list3(10, 'a');
	cout << "list3: ";
	PrintList(list3);

	list<char> list4(list3);
	cout << "list4: ";
	PrintList(list4);
	

}


void main()
{
    
    
	_01Test01();
}

list assignment and exchange

Function:

  • Assign values ​​to the list container and exchange the list container
    function prototype:
  • assign(beg,end); //Assign the data copy in [beg,end] range to itself
  • assign(n, elem); //Assign n copies of elem to itself
  • list& operator=(const list &list); // overload assignment operator
  • swap(lst); //exchange lst with its own elements
    example:
#include<iostream>
#include <list>
using namespace std;

//赋值和交换
void PrintList02(list<int>& l)
{
    
    
	for (list<int>::iterator it = l.begin(); it != l.end(); it++)
	{
    
    
		cout << *it << "  ";
	}
	cout << endl;
}

void _02Test01()
{
    
    
	list<int> l1;
	for (int i = 0; i < 10; i++)
	{
    
    
		l1.push_back(i);
	}
	cout << "l1:  ";
	PrintList02(l1);

	list<int> l2;
	l2.assign(++l1.begin(), --l1.end());
	cout << "l2:  ";
	PrintList02(l2);

	list<int> l3(10, 100);
	cout << "l3:  ";
	PrintList02(l3);

	list<int> l4;
	l4 = l3;
	cout << "l4:  ";
	PrintList02(l4);

	l1.swap(l4);
	cout << "l1 和 l4 交换后:" << endl;
	cout << "l1:  ";
	PrintList02(l1);
	cout << "l4:  ";
	PrintList02(l4);

}

void main()
{
    
    
	_02Test01();
}

list size operations

Function:

  • The function prototype of the size operation on the list container
    :
  • size(); // number of elements
  • empty(); //Is the container empty
  • resize(int num); //Respecify the length of the container as num, if the container becomes longer, the default value is 0 to fill the new position. If the container gets shorter, elements at the end that exceed the length of the container are removed
  • resize(int num, elem); //Respecify the length of the container as num, if the container becomes longer, fill the new position with the value of elem, if the container becomes shorter, delete the elements whose end exceeds the length of the container

list insertion and deletion

Function:
Insert and delete operations on the list container

Function prototype:

Insert operation at both ends:

  • push_back(elem); //Insert a data at the end of the container
  • push_front(elem); //Insert a data at the head of the container
  • pop_back(); //Delete the last data in the container
  • pop_front(); //Delete the first data of the container

Specified position operation:

  • insert(pos,elem); //The iterator points to the position pos to insert an element elem, and returns the position of the new data
  • insert(pos, n, elem) //The iterator points to position pos to insert n elements elem, no return value
  • insert(pos, beg, end); //Insert the data in [beg, end] range at position pos, no return value
  • erase(const_iterator pos); //Delete the element pointed to by the iterator
  • erase(const_iterator start, const_iterator end); //Delete elements between start and end of the iterator
  • clear(); // delete all elements in the container
  • remove(elem); //Delete all elements in the container that match the elem value

list data access

Function description:

  • Access the data in the list container

Function prototype:

  • front(); // return the first element
  • back(); // return the last element

list reverse and sort

Function:

  • Reverse the elements in the container and sort the data in the container

Function prototype:

  • reverse(); // reverse linked list
  • sort(); //List sorting

Example:

#include<iostream>
using namespace std;
#include <list>
//list容器反转和排序
//-reverse();    //反转链表
//-sort();    //链表排序
void PrintList06(list<int>& l)
{
    
    
	for (list<int>::iterator it = l.begin(); it != l.end(); it++)
	{
    
    
		cout << *it << "  ";
	}
	cout << endl;
}

void _06Test01()
{
    
    
	list<int> l1;
	for (int i = 0; i < 10; i++)
	{
    
    
		l1.push_back(i);
	}
	cout << "l1:  ";
	PrintList06(l1);

	l1.reverse(); cout << "反转后:\nl1:  ";
	PrintList06(l1);

	l1.push_back(12);
	l1.push_back(31);
	l1.push_back(3);
	l1.push_back(100);
	l1.sort();
	l1.reverse(); cout << "排序后:\nl1:  ";
	PrintList06(l1);
}

void main()
{
    
    
	_06Test01();
}

sort case

Case description: Sort the custom data types of Person. Person has the attributes name, height, and age.
Sorting rule: sort by age in ascending order, and then sort by height in descending order if the age is the same
. Example:

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

//list容器 排序案例
class Person
{
    
    
public:
	string m_Name;
	int m_Age;
	int m_Height;
public:
	Person(string name, int age, int height)
	{
    
    
		this->m_Age = age;
		this->m_Height = height;
		this->m_Name = name;
	}
};

void PrintPerson(list<Person> &l)
{
    
    
	for (list<Person>::iterator it = l.begin(); it != l.end(); it++)
	{
    
    
		cout << "姓名: " << it->m_Name << " 年龄:" << it->m_Age << " 身高:" << it->m_Height << endl;
	}
}

//指定排序规则
bool SortRule(Person p1, Person p2)
{
    
    
	if (p1.m_Age == p2.m_Age)
	{
    
    
		return p1.m_Height > p2.m_Height;
	}
	else
	{
    
    
		return p1.m_Age < p2.m_Age;
	}
}

void _07Test01()
{
    
    
	Person p1("张三", 18, 175);
	Person p2("李四", 18, 180);
	Person p3("王五", 17, 181);
	Person p4("麻子", 18, 171);
	Person p5("赵六", 20, 190);

	list<Person> l;
	l.push_back(p1);
	l.push_back(p2);
	l.push_back(p3);
	l.push_back(p4);
	l.push_back(p5);

	PrintPerson(l);
	cout << "排序后: " << endl;
	l.sort(SortRule);
	PrintPerson(l);
}

void main()
{
    
    
	_07Test01();
}

Guess you like

Origin blog.csdn.net/qq_60406853/article/details/125291327