Use of the C + + Standard Template Library (STL) doubly linked list (list)

Source: Internet
Author: User

A doubly linked list is a common data structure. It's not complicated, and it's not too difficult if we're going to make it ourselves. But since the STL has provided us with one, it may be used directly. To do so, not only saves time and effort, but also the reusability of code.


Header files and template classes

To use a doubly linked list provided by STL, you need to include a header file

#include <list>

This allows you to use the template class list<t>.


Initialization

Initializing a list is simple, using

Std::list<t> L;

You can initialize an empty linked list. Use

std::list<t> l{T1, T2, T3};

You can initialize a linked list with three elements.

For example

Std::list<int> l1{20, 13};

Then L1 is a linked list with 2 int. Again, for example,

std::list<std::string> l2{"Real variable function", "Functional Analysis", "differential geometry", "Abstract algebra"};

Then L2 is a linked list with 4 strings.


Add and delete elements

List<t> 's member function Push_back,push_front,pop_back, Pop_front's role is to add elements at the tail, add elements in the head, delete elements at the tail, delete elements in the head.

The first two function's parameter type is (t&&) (C++11 's "Move Semantics"), the latter two functions have no parameters.

None of the 4 functions have a return value.


Sizes (size)

The size of a list refers to how many elements are in the list, not how many bytes the list occupies in memory.

Gets the size of the list of tables that can be used with the list<t> member function size. It has no parameters and the return value is size_t.


Iterators

An iterator is a class that expresses a position. The following code doubles all elements of L1.

for (Auto it = L1.begin (); It! = L1.cend (); ++it) {(*it) *= 2;}


Removing an element from an iterator

You can use the function list<t>::erase to remove an element from an iterator. Its parameters are iterators that point to the element to be deleted.

Use of the C + + Standard Template Library (STL) doubly linked list (list)

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.