Use of list in STL

Source: Internet
Author: User

From: http://apps.hi.baidu.com/share/detail/34671110

The list in STL is a two-way linked list that can efficiently insert and delete elements. Here we will summarize its operations.

In this paper, the two List objects C1 and C2 have the element C1 (10, 20, 30) C2 (40, 50, 60 ). There is also a list <int>: iterator citer that points to the C1 or C2 element.

Declaration structure of list object ():

A. List <int> C0; // empty linked list

B. List <int> C1 (3); // create a linked list containing three elements whose default value is 0.

C. List <int> C2 (5, 2); // create a linked list containing five elements. The values are 2.

D. List <int> C4 (C2); // create a copy linked list of C2.

E. List <int> C5 (c1.begin (), c1.end (); // C5 contains the element [_ first, _ last) of a C1 region ).

1. Assign () allocation Value

There are two reloads:

C1.assign (++ c2.begin (), c2.end () // C1 is now (50, 60 ).

C1.assing () // C1 is now 7 4, that is, C1 = (, 4, 4 ).

2. Back () returns the reference of the last element.

Int I = c1.back (); // I = 30

Const int I = c2.back (); // I = 60 and cannot be modified

3. Begin () returns the pointer to the first element (iterator)

Citer = c1.begin (); // * citer = 10

List <int>: const_iterator cciter = c1.begin (); // * cciter = 10 and Const.

4. Clear () delete all elements

C1.clear (); // C1 is null. c1.size is 0;

5. Empty () determines whether the linked list is empty.

Bool B = c1.empty (); // If C1 is empty, B = true; otherwise, B = false;

6. End () returns the pointer to the next position of the last element.(Usually used to determine whether the traversal ends)

(If list is empty, end () = begin ())

Citer = c1.end (); // * (-- citer) = 30;

Same as begin (), a constant pointer is returned, and elements cannot be modified.

7. Erase () delete an element or an element of a region (two reloads)

C1.erase (c1.begin (); // C1 (20, 30 );

C1.erase (++ c1.begin (), c1.end (); // C1 is currently (10 );

8. Front () returns the reference of the first element.

Int I = c1.front (); // I = 10;

Const int I = c1.front (); // I = 10 and cannot be modified.

9. insert () insert one or more elements at the specified position (three reloads)

C1.insert (++ c1.begin (), 100); // C1 (10,100, 20, 30)

C1.insert (c1.begin (), 2,200); // C1 (200,200, 20, 30 );

C1.insert (++ c1.begin (), c2.begin (), -- c2.end (); // C1 (10, 40, 50, 20, 30 );

10. max_size () returns the maximum possible length of the linked list (size_type is int type)

List <int>: size_type I = c1.max _ SIZE (); // I = 1073741823

11. Merge () merges two linked lists and uses them in ascending order by default (you can also change them)

C2.merge (C1); // C1 is empty; C2 is C2 (10, 20, 30, 40, 50, 60)

C2.merge (C1, greater <int> (); // same as above, but C2 is in descending order

12. pop_back () deletes an element at the end of the linked list.

C1.pop _ back () // C1 (10, 20 );

13. pop_front () deletes an element from the linked list header.

C1.pop _ Front () // C1 (20, 30)

14. push_back () adds an element to the end of the linked list.

C1.push _ back (100) // C1 (10, 20, 30,100)

15. Add an element to the linked list header in push_front ().

C1.push _ Front (100) // C1 (, 10, 20, 30)

16. rbegin () returns the backward pointer (reverse_iterator or const) of the last element of the linked list)

List <int>: reverse_iterator riter = c1.rbegin (); // * riter = 30

17. rend () returns the backward pointer to the next position of the first element of the linked list.

List <int>: reverse_iterator riter = c1.rend (); // * (-- riter) = 10

18. Remove () delete elements with matched values in the linked list(Delete all matching elements)

C1.remove (10); // C1 (20, 30)

19. remove_if () deletes elements that meet the conditions.(The linked list will be traversed)

C1.remove _ if (is_odd <int> (); // C1 (10, 20, 30)

// Write is_odd by yourself (odd table)

20. Resize () redefinition of the linked list length (two overloading)

C1.resize (4) // C1 (10, 20, 30, 0) fill with the default value

C1.resize (4,100) // C1 (30,100,) fill with the specified value

21. Reverse () reverse linked list

C1.reverse (); // C1 (30,20, 10)

22. Size () returns the number of elements in the linked list.

List <int>: size_type I = c1.size (); // I = 3

23. Sort () sorts linked lists in ascending order by default (customizable)

C1.sort (); // C1 (10, 20, 30)

C1.sort (Great <int> (); // C1 (30,20, 10)

24. splice () combines two linked lists(Three reloads)

C1.splice (++ c1.begin (), C2); // C1 (10, 40, 50, 60, 20, 30) C2 is empty and fully merged

C1.splice (++ c1.begin (), C2, ++ c2.begin (); // C1 (10, 50, 20, 30); C2 (40, 60) specify element Merging

C1.splice (++ c1.begin (), C2, ++ c2.begin (), c2.end (); // C1 (10, 50, 60, 20, 30); C2 (40) specified range merge

25. Swap () exchange two linked lists(Two reloads)

C1.swap (C2); // C1 (40, 50, 60 );

Swap (C1, C2); // C1 (40, 50, 60)

26. Unique () deletes adjacent duplicate elements(Assertion has been sorted because it does not delete non-adjacent identical elements)

C1.unique ();

// Assume that C1 (-10, 10, 10, 20,-10) is followed by C1 (-10, 10, 20,-10)

C1.unique (mypred); // custom Predicate

Note: The insert function is a combination of the right side. Insert (J, * (-- j) is performed from the rightmost to the left.

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.