List (articles)

First of all, why introduce LIst? A vector is a dynamic array, which means that it contains the advantages and disadvantages of the array. Advantages: the memory is continuous, and if you know the first address, you can deduce the value of other locations. Disadvantage: Inserting and removing elements requires incidental movement. If there are too many elements in the vector, the efficiency will be very low. At this time, the memory address of the List is not continuous, which solves the problem of insertion and deletion very well. But in the same way, although LIst solves the shortcomings of Vector, List cannot directly determine the element at the specified position, it needs to traverse the linked list from the beginning, and the performance of random access is poor.

Less code does not mean that there is less content in it, nor is it perfunctory. After learning vector, I feel that there is little difference between the two, which is roughly the same and can be applied.

//List 
	list<int> l1(4,100);
	list<int> l2(l1);
	//遍历
	list<int> ::iterator it;
	for (it = l1.begin(); it != l1.end(); it++)
	{
    
    
		cout << *it << " ";
	}
	cout << endl;

	//普通数组初始化
	int a[] = {
    
     2,5,8,10 };
	list<int> l3(a, a + sizeof(a) / sizeof(int));

	list<int> ::iterator it3;
	for (it3 = l3.begin(); it3 != l3.end(); it3++)
	{
    
    
		cout << *it3 << " ";
	}
	cout << endl;

	//insert()

	l3.insert(l3.begin(), 2, 11);

	list<int> ::iterator it5;
	for (it5 = l3.begin(); it5 != l3.end(); it5++)
	{
    
    
		cout << *it5 << " ";
	}
	cout << endl;


	//reverse() 倒置
	l3.reverse();
	list<int> ::iterator it4;
	for (it4 = l3.begin(); it4 != l3.end(); it4++)
	{
    
    
		cout << *it4 << " ";
	}
	cout << endl;

在这里插入代码片

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324109757&siteId=291194637