Basic knowledge of deque and list in C + +

Source: Internet
Author: User

The constructors of deque and vectors in STL, assign are basically the same, focusing on the characteristics of deque, the main differences between deque and vectors are as follows:

Deque is the data structure of a two-terminal queue, which can efficiently add and remove elements at the head and end of the team, which is relative to the vector's advantage.

Deque internally uses segmented contiguous memory space to store elements, and can add new space and link up at any time when inserting elements, so although random access is provided, the speed of access is slower than vector.

Deque does not have the data function because the deque element is not placed in the array.

Deque does not provide capacity and reserve operations. Inserts and deletes elements within the deque are slower than the list.

Since deque is not the most efficient in performance, sometimes sorting deque elements, it is more efficient to move deque elements to vectors and then sort them, and then move back in.

Deque in order to realize the illusion of the overall continuity, which makes it more cumbersome to implement, as little as possible to use it.

The realization principle of deque: http://blog.csdn.net/baidu_28312631/article/details/48000123, this article is very clear, can refer to.
Deque Source Analysis: http://blog.csdn.net/terence1212/article/details/52326945, this article can refer to the initialization of deque deque Deque initialization, see the following code. The initialization of deque is exactly the same as the vector.

Deque's assign function, there are also three ways to initialize the list, assign values of several identical values, and the range of any sequence container

Deque<int> v2;        Deque with 0 element
deque<int> v3 (m);   Deque with elements whose value
is 0 deque<int> v4 (5);//deque with elements whose value is 5
D Eque<int> v5{20}; Deque with 1 element whose the value is
deque<int> V6 (v4);   Copy construct function
deque<int> v7 (V4.begin (), V4.end ());//construct function
deque<int> V8 ({1,2,3,4,5});//Construct a deque with initialize list deque<int> v9{1,2,3,4,5};//construct
a deque with I Nitialize list
deque<int> v10 = {1,2,3,4,5};//assignment function
Deque's Assign

There are three forms of deque assign, assign

Deque<int> Mydeque;
Deque<int> temp{1,2,4,5,6};
Mydeque.assign (Temp.begin (), Temp.end ());   The range of the receiving Sequence container
mydeque.assign (3, 5);      becomes 3 values, each value is 5
mydeque.assign ({1,2,3,4,5,6});    Assign a list of initialization
add element for dequeAdd elements to the deque, in addition to Push_back and Emplace_back, because it is a two-terminal queue, you can add elements to the head of the team, Deque Push_front and Emplace_front and so on.
Deque<int> Mydeque;
Mydeque.push_back (1);
Mydeque.push_front (2);
Mydeque.emplace_back (3);
Mydeque.emplace_front (6);
Copy (Mydeque.begin (), Mydeque.end (), ostream_iterator<double> (cout, ""));
List List Features

The elements of list and deque are in different forms of memory and are saved as linked lists.

The list and deque are the same, and elements can be added to the tail and head, and therefore also support push_back and Push_front as well as Emplace_front and Emplace_back

List is an efficient way to insert elements internally without moving elements, list is a two-way list

The Insert function of the list and the Emplace and Vector,deque are exactly the same as the list. some function list unique member functions remove () can remove elements that match the parameters. For example, the following code:

Std::list<int> numbers{2,5,2,3,6,7,8,2,9};
Numbers.remove (2),//delete element copy of element value 2 (
numbers.begin (), Numbers.end (), ostream_iterator<int> (cout, ""));
member function remove_if () expects to pass in a unary assertion as an argument, a unary assertion accepts a parameter or reference of the same type as the element, and returns a Boolean value. An element that asserts that returns true will be removed. An assertion can be a function, or it can be a lambda expression.
Std::list<int> numbers{2,5,2,3,6,7,8,2,9};
Auto F = [] (const int& i)->bool {return I% 2 = 0;};
Numbers.remove_if (f);//delete all even
copy (Numbers.begin (), Numbers.end (), ostream_iterator<int> (cout, ""));
The member function is unique and can be removed Continuous Repetitionelement, leaving only one, but the function cannot delete the discontinuous duplicate elements in the list. Therefore, if you need to remove duplicate elements, you need to sort, sort, and then delete the duplicate elements.
The unique () function overloads two forms, a version with no arguments, and a parameter that accepts a two-dollar assertion. An element that asserts that returns true is considered equal.
No sort, direct unique
numbers.assign ({2,5,2,4,7,7,7,8});
Numbers.unique ()//{2,5,2,4,7,8} did not remove duplicate but discontinuous element
copy (Numbers.begin (), Numbers.end (), Ostream_iterator<int > (cout, ""));

The unique
numbers.assign ({2,5,2,4,7,7,7,8}) after sort;
Numbers.sort ()///2,2,4,5,7,7,7,8
Numbers.unique ()//2,4,5,7,8
copy (Numbers.begin (), Numbers.end (), Ostream_iterator<int> (cout, ""));


The unique overloaded form
numbers.assign ({2,5,2,4,7,7,7,8});
Numbers.sort ();//2 2 4 5 7 7 7 8
Auto f1 = [] (const int& A, const int& B)->bool {return a% 2 = b% 2;} ;
Numbers.unique (F1);//2 5 8
copy (Numbers.begin (), Numbers.end (), ostream_iterator<int> (cout, ""));
The sort () function has two versions: sort () without parameters arranges all the elements in ascending order, and the sort () function with the parameter version accepts a function object or a lambda function to indicate the sort basis.
Use assertion
numbers.assign ({2,5,2,4,7,7,7,8});
Numbers.sort (greater<> ());//2 2 4 5 7 7 7 8
//using lambda function
numbers.assign ({2,5,2,4,7,7,7,8});
Auto F3 = [] (const int&a, const int& b)->bool{return a>b;};
Numbers.sort (F3);//2 2 4 5 7 7 7 8

The above example uses the template greater<t> assertion in functional, which can be in the form of gerater<int> or greater<>, which is a compact version of the function object, You can accept any type of argument, using perfect forwarding (perfect forwarding) to avoid parameter copies, and the parameters being compared will be moved rather than copied into the function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.