C ++ | STL list container Talking

table of Contents

A. Brief list container

Two ways to create a container .list

Three .list container insert and delete operations

IV. Use a container iterators on list


A. Brief list container

 list is a doubly linked list container, which means it is a low-level two-way circular list. Header file is #include <list>

Because the list is a doubly linked list container, so that a continuous logical address, physical address but not continuous, so we can not use the pointer jumps directly to access the elements of a doubly linked list. For example, in the figure above, arr pointer pointing to the first node of a doubly linked list, the second element when we want to access the doubly linked list, it can not be * (arr + 2) to access this manner, it is also we mean follow-up of the iterator access list container can not be accessed using iterator + i (iterator behalf iterator, i = 0,1,2,3 ..... n) in this way.

Two ways to create a container .list

#include<iostream>
#include<vector>
#include<list>
#include<functional>

int main()
{
	std::list<int> lst1;
	std::list<int> lst2(10);//count
	std::list<int> lst3(10, 20);//count, value
	std::list<int> lst4(lst3.begin(), lst3.end());

	return 0;
}

In constructing the container list lst1, calling the class list is the default constructor (all containers have a default constructor), while the construction and did not do anything.

In constructing the container lst2 list, we explicitly given the initial size of the container 10, 10 and the size of these data is set to 0.

In constructing the container list lst3, we explicitly given the initial size of the container 10, and these explicit data set of size 10 to 20. I.e., we insert 10 to the data value 20 in lst3.

In constructing the container lst4 list, we pass the parameter is an iterator-range, a position after the start and end of the upcoming lst3 incoming, which is inserted in the data lst3 to lst4 in.

Three .list container insert and delete operations

#include<iostream>
#include<vector>
#include<list>
#include<functional>

template<typename Iterator>
void Show(Iterator first, Iterator last)
{
	for (first; first != last; first++)
	{
		std::cout << *first << " ";
	}
	std::cout << std::endl;
}

int main()
{
	std::list<int> lst1;


	for (int i = 0; i < 3; i++)
	{
		lst1.push_back(i + 1);
	}// 1 2 3
	for (int i = 0; i < 3; i++)//3 2 1 1 2 3
	{
		lst1.push_front(i + 1);
	}
	Show(lst1.begin(), lst1.end());

	lst1.insert(++lst1.begin(), 6);
	Show(lst1.begin(), lst1.end());

	lst1.pop_front();
	Show(lst1.begin(), lst1.end());

	lst1.pop_back();
	Show(lst1.begin(), lst1.end());

	lst1.erase(++lst1.begin(), --lst1.end());
	Show(lst1.begin(), lst1.end());

	return 0;
}

 

push_back i.e. the end plug inserted in the end of the list of elements, the time complexity of O (. 1) .

push_front i.e. insertion head, inserted in the head element of the list, the time complexity of O (. 1) .

insert elements i.e. by location to insert, insert the following five functions overload, time complexity of O (1)

  1. iterator std :: list <_Ty, _Alloc> :: insert <_Iter,> (const const_iterator _Where, _Iter _First, _Iter _Last), by location (Where) (Iter_First ~ Iter_Last) inside the element into a section, the section represented by is an iterator range.
  2. iterator std :: list <_Ty, _Alloc> :: insert (const_iterator _Where, size_type _Count, const _Ty & _Val), inserts Count value Val at position Where this element.
  3. iterator std :: list <_Ty, _Alloc> :: insert (const_iterator _Where, const _Ty & _Val), Where in this position to insert an element Val value, and this value is modified const locked, it can not be modified.
  4. iterator std :: list <_Ty, _Alloc> :: insert (const_iterator _Where, _Ty && _Val), Where in this position to insert an element Val value, but this value is not modified lock const.
  5. iterator std :: list <_Ty, _Alloc> :: insert (const_iterator _Where, initializer_list <_Ty> _Ilist), use the initialization element in the list to insert a new position Where, e.g.
     lst1.insert(lst1.begin(),{1,3,4});

     

That head pop_front delete, delete the list of the first element, the time complexity of O (1) .

That tail pop_back delete, delete the list of trailing elements, the time complexity of O (. 1) .

i.e. erase position according to delete, erase function has the following two overloaded, the time complexity of O (1)

  1. iterator erase (const const_iterator _First, const const_iterator _Last), delete all the elements of a range of iterators.
  2.  iterator erase (const const_iterator _Where), remove elements Where this position.

Access, i.e. Show () function of the time complexity of O (n-) .

So list the advantages of container is "rapid insertion or deletion anywhere", the disadvantage is "less efficient access."

IV. Use a container iterators on list

#include<iostream>
#include<vector>
#include<list>
#include<functional>

int main()
{
	int arr[] = { 1, 3, 214, 23, 5, 3 };
	int len = sizeof(arr) / sizeof(arr[0]);

	std::vector<int> vec(arr, arr + len);//内存连续   随机访问迭代器
	std::list<int> lst(vec.begin(), vec.end());//内存不连续	双向迭代器  ++ --	

	std::cout << vec[2] << std::endl;
	//std::cout << lst[1] << std::endl;

	std::vector<int>::iterator vit = vec.begin() + 2;
	std::cout << *vit << std::endl;

	//std::list<int>::iterator  lit = lst.begin() + 2;
        //std::cout << *lit << std::endl;

	return 0;
}

 

We are aware of the underlying container vector is an array, its logical and physical addresses are continuous, so we can use to access the data pointer plus an offset, and the physical address of the address list container is discontinuous, so we can not plus an offset pointer to access the data. We vector container iterator known as random access iterators, container and list iterator called bidirectional iterator.

Popular speaking, bidirectional iterator can not + i or -i operation, if you want to traverse the container, then it can only be + or - operation. (I = 1,2,3 .... n)

And random access iterators, + i or -i perform both operations can be performed ++ or - operations. (I = 1,2,3 .... n)

Published 61 original articles · won praise 26 · views 5798

Guess you like

Origin blog.csdn.net/ThinPikachu/article/details/104906387
Recommended