Detailed description of VC ++ list functions and vclist Functions

Source: Internet
Author: User

Detailed description of VC ++ list functions and vclist Functions

Before using it, you need to do two things:

(1) # include <list>

(2) using namespace std;

Fame variable:

List <int> intlist;

I. constructor, destructor, = Operator

1. function: declare the list container. Four Methods

List <int> first; // empty list of ints

List <int> second (4,100); // four ints with value 100. Four 100

List <int> third (second. begin (), second. end (); // iterating through second

List <int> fourth (third); // a copy of third

2. function: deregister the list. ~ List ();

3. prototype: list1 = list2;

Function: Assign list2 to list1, including all the elements of the list and the size of list2.

Returned value: this pointer

2. Return functions of the iterator class

Begin, end, rbegin, rend

Example:

Begin points to the first element, a yellow arrow. End is the last position of the last element, with a black arrow. Begin and end are generally used together and the list is output in the forward order. Rbegin refers to the first element in reverse order, that is, the last element, and the blue arrow. Rend refers to the first position of the last element in reverse order, that is, the first position of the first element, and the Red Arrow.

Rbegin and rend are generally used together to output the list in reverse order.

Iii. list capacity-related functions

1. empty

Prototype: bool empty () const;

Function: determines whether lsit is null, that is, whether the size is 0.

Return Value: 0. true is returned. Otherwise, false is returned.

2. size

Prototype: size_type size () const;

Function: returns the number of elements in lsit.

Returned value: size_type

3. Max_size

Prototype: size_type max_size () const;

Function: returns the maximum lsit capacity.

Return Value:

4. resize

Prototype: void resize (size_type sz, T c = T ());

Function: re-allocate the lsit size. If the sz is smaller than the current size, the redundant value is deleted. If the sz is larger than the current size, the capacity is increased and filled with c. For example:

Mylist. resize (5); // set the size to 5.
Mylist. resize (8,100); // set the size to 8, and fill the remaining values with 100.
Mylist. resize (12); // set the size to 12.

4. Obtain Elements

1. front

Prototype: reference front ();
                         const_reference front ( ) const; 

Function: gets the first element.

Returned value: the value of the first element.

2. back

Prototype: reference back ();
const_reference back ( ) const 

Function: gets the last element.

Returned value: the last element.

5. Modify lsit Functions

1. assign

Prototype: void assign (InputIterator first, InputIterator last );
               void assign ( size_type n, const T& u) 

Function: reallocates space for the list and assigns a value. Assign the value in the range of [first, last) or n copies of the u value to the list.

Return Value: None

2. push_front: insert an element from the beginning. Pop_front: deletes the first element.

Push_back: insert an element at the end. Pop_back: Delete the last element.

3. insert

Prototype: iterator insert (iterator position, const T & x );
    void insert ( iterator position, size_type n, const T& x ); 
template <class InputIterator> 
    void insert ( iterator position, InputIterator first, InputIterator last ); 

Function: insert an element.

Insert (iterator position, const T & x): insert element x at position

Insert (iterator position, size_type n, const T & x): insert n x at position

Insert (iterator position, InputIterator first, InputIterator last): insert at position

Elements in the range of [first, last.

Returned value: only the first function returns the position of the inserted element.

4. erase

Prototype: iterator erase (iterator position );

Iterator erase (iterator first, iterator last );

Function: clears the elements in the position or [first, last) range of the linked list. The size of the list is reduced.

Return Value: next position of the last element to be cleared (iterator)

5. swap

Prototype: void swap (list <T, Allocator> & lst)

Function: switch two lsit instances.

6. clear

Function: clears the list.

Vi. operation functions

1. splice

Prototype: Set list2 to call the splice function.

Void splice (iterator position, list <T, Allocator> & x); inserts all elements in list x into the position of list2 that calls this function. List x is cleared.

Void splice (iterator position, list <T, Allocator> & x, iterator I); inserts the elements pointing to I in x into the position of list2. X deletes the value at position I.

Void splice (iterator position, list <T, Allocator> & x, iterator first, iterator last); Convert [first, last) in x) insert the element at the position to the position of list2.

Function: Move elements from list to list. Move the value in one lsit to another list

2. remove

Prototype: void remove (const T & value );

Function: Clear the specific value in the linked list. The size of lsit is reduced accordingly.

Return Value: None

3. remove_if

Prototype: template <class Predicate>

Void remove_if (Predicate pred );

Function: removes an element when the Predicate pred returns true. Pred can be a function that returns the bool type, or a class that overrides the operator function. For example:

// A predicate implemented as a function:

Bool single_digit (const int & value) {return (value <10 );}

// A predicate implemented as a class:

Class is_odd

{

Public:

Bool operator () (const int & value) {return (value % 2) = 1 ;}

};

Return Value: None

4. unique

Prototype: void unique ();

Template <class BinaryPredicate>

Void unique (BinaryPredicate binary_pred); eliminate duplicate values according to the rule binary_pred. For example:

bool same_integral_part (double first, double second) 
{ return ( int(first)==int(second) ); } 
  
// a binary predicate implemented as a class: 
class is_near 
{ 
public: 
  bool operator() (double first, double second) 
  { return (fabs(first-second)<5.0); } 
}; 
Call: mylist. unique (same_integral_part );

Mylist. unique (is_near ());

Function: removes repeated elements in the list.

Return Value:

5. merge

Prototype: void merge (list <T, Allocator> & x );

Template <class Compare>

Void merge (list <T, Allocator> & x, Compare comp );

Function: combines two sorted lists (both in ascending or descending order.

Merge () combines two sorted tables. If a table is not sorted, merge () can still produce a table that contains the union of the original two table elements. Of course, there is no guarantee for sorting the results. Like the splice () function, the merge () function does not copy elements.

The merge function is used to combine two ordered sequences into an ordered sequence. Function parameters: merge (first1, last1, first2, last2, result, compare); // firs1t is the first iterator of the first container, and last1 is the last iterator of the first container, first2 is the first iterator of the second container, last2 is the last iterator of the container, result is the container that stores the result, and comapre is the comparison function (can be omitted, is merged into an ascending sequence by default ).

Return Value:

6. sort

Prototype: void sort ();

Template <class Compare>

Void sort (Compare comp );

Function: Sort

Return Value:

7. reverse

Function: reverse the element in the list.

Return Value:

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.