Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use list in C++

Shulou Source: shulou.com Published: 2022-06-02 13:33:18 10月22日 Update

This article mainly explains "how to use list in C++". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use list in C++.

The use of structures

There are four main constructors, which are as follows

List () constructs empty listlist (size_type n, const value_type& val = value_type ()) the list contains n elements list (const list& x) with the value of val copy constructor list (InputIterator first, InputIterator last) constructs list1 to construct empty list with elements in [first, last) interval

Do not need to pass in any parameters, directly use the list class template to define the object

List L1; / define int linked list list L2; / define char linked list list L3; / / define val linked list / / three objects above, all empty 2 to construct elements with n values of char

You can pass parameters directly according to the above definition.

List L1 (4 list 5); / / defines an int linked list with 4 5 list L2 (3 recordings); / / defines a char linked list with 3's'L3 (4 list 3); / / defines an int linked list with 4 2.33 copy constructions

That is, pass in a list of the same type

List L1 (4 list 5); / / defines an int linked list containing 4 5 L2 (L1); / / makes a copy of L1 and gives it to L24 with iterative intervals

* * there is a note here, the iteration interval is left closed and right open! * * that does not include the right boundary.

Int num [4] = {1rec 2je 3je 4}; list L1 (3dhow`); list L2 (l1.begin (), l1.end ()); / / end () is the next element position of the last element position, so it does not include, so the content of L2 is' w`w' 'w'list L3 (num,num + 3). / / because of the location of num+3, the index is 3, but the iteration interval is left closed and right open, so the index 3 position is not included, and the content is 1 2 3 iterator interface.

C++ provides the following:

Function declaration interface description begin () + end () returns the iterator of the first element + iterator rbegin () + rend () returns the reverse_iterator of the next position of the last element, that is, the end position + returns the reverse_iterator of the next position of the last element, that is, the begin position 1 normal iterative interface int num [5] = {1, 2, 3, 4, 5}; list li (num,num+5) / / create a linked list with content 1 2 3 4 5 list::iterator it = li.begin (); while (it = li.end ()) {cout

Tags: Element content location iteration interface function interval two clocked data result output three parameter object copy template index facet learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL vpn OPPO Reno Redmi Linux