C ++ basic related STL: List usage

Source: Internet
Author: User

Lists stores elements in the linked list sequentially. Compared with vectors, lists allows fast insertion and deletion, but random access is slow.

Assign () assigns a value to the list
Back () returns the last element
Begin () returns the iterator pointing to the first element.
Clear () delete all elements
Empty () returns true if list is empty.
End () returns the iterator at the end.
Erase () deletes an element.
Front () returns the first element
Get_allocator () returns the list Configurator
Insert () inserts an element into the list.
Max_size () returns the maximum number of elements that a list can hold.
Merge () combines two lists
Pop_back () Delete the last element
Pop_front () deletes the first element
Push_back () adds an element to the end of the list.
Push_front () adds an element to the list header.
Rbegin () returns the reverse iterator pointing to the first element.
Remove () delete an element from the list
Remove_if () deletes an element based on the specified conditions.
Rend () points to the reverse iterator at the end of the list
Resize () changes the list size.
Reverse () Reverse the list element
Size () returns the number of elements in the list.
Sort () sorts the list
Splice () merges two lists
Swap () exchange two lists
Unique () deletes repeated elements in the list.

Attached List usage example:

# Include <iostream>
# Include <list>
# Include <numeric>
# Include <algorithm>

Using namespace STD;

// Create a list container instance listint
Typedef list <int> listint;

// Create a list container instance listchar
Typedef list <char> listchar;

Void main (void)
{
//--------------------------
// Use the list container to process Integer Data
//--------------------------
// Use listint to create a list object named listone
Listint listone;
// Declare I as an iterator
Listint: iterator I;

// Add data to the previous listone container
Listone. push_front (2 );
Listone. push_front (1 );

// Add data to the listone container
Listone. push_back (3 );
Listone. push_back (4 );

// Display the data in listone from the front to the back
Cout <"listone. Begin () --- listone. End ():" <Endl;
For (I = listone. Begin (); I! = Listone. End (); ++ I)
Cout <* I <"";
Cout <Endl;

// Display the data in listone from the back to the back
Listint: reverse_iterator IR;
Cout <"listone. rbegin () --- listone. rend ():" <Endl;
For (IR = listone. rbegin (); IR! = Listone. rend (); IR ++ ){
Cout <* IR <"";
}
Cout <Endl;

// Use the STL accumulate (accumulate) Algorithm
Int result = accumulate (listone. Begin (), listone. End (), 0 );
Cout <"sum =" <result <Endl;
Cout <"------------------" <Endl;

//--------------------------
// Use the list container to process sorted data
//--------------------------

// Use listchar to create a list object named listone
Listchar listtwo;
// Declare I as an iterator
Listchar: iterator J;

// Add data to the previously oriented listtwo container
Listtwo. push_front ('A ');
Listtwo. push_front ('B ');

// Add data to the listtwo container
Listtwo. push_back ('x ');
Listtwo. push_back ('y ');

// Display the data in listtwo from the beginning to the back
Cout <"listtwo. Begin () --- listtwo. End ():" <Endl;
For (j = listtwo. Begin (); J! = Listtwo. End (); ++ J)
Cout <char (* j) <"";
Cout <Endl;

// Use the max_element algorithm of STL to calculate the maximum element in listtwo and display
J = max_element (listtwo. Begin (), listtwo. End ());
Cout <"the maximum element in listtwo is:" <char (* j) <Endl;
}

# Include <iostream>
# Include <list>

Using namespace STD;
Typedef list <int> intlist;

// Display all elements of the list queue from the beginning to the back
Void put_list (intlist list, char * name)
{
Intlist: iterator plist;

Cout <"the contents of" <name <":";
For (plist = List. Begin (); plist! = List. End (); plist ++)
Cout <* plist <"";
Cout <Endl;
}

// Test the list container Function
Void main (void)
{
// The list1 object is empty.
Intlist list1;
// The list2 object initially has 10 elements with a value of 6.
Intlist list2 (10, 6 );
// The list3 object initially has three elements with a value of 6.
Intlist list3 (list2.begin (), -- list2.end ());

// Declare a bidirectional iterator named I
Intlist: iterator I;

// Display the elements of each list object from the front to the back
Put_list (list1, "list1 ");
Put_list (list2, "list2 ");
Put_list (list3, "list3 ");

// Add two elements after the list1 Sequence
List1.push _ back (2 );
List1.push _ back (4 );
Cout <"list1.push _ back (2) and list1.push _ back (4):" <Endl;
Put_list (list1, "list1 ");

// Add two elements from the front of the list1 Sequence
List1.push _ Front (5 );
List1.push _ Front (7 );
Cout <"list1.push _ Front (5) and list1.push _ Front (7):" <Endl;
Put_list (list1, "list1 ");

// Insert data in the middle of the list1 Sequence
List1.insert (++ list1.begin (), 3,9 );
Cout <"list1.insert (list1.begin () + 1, 3, 9):" <Endl;
Put_list (list1, "list1 ");

// Test the reference class function
Cout <"list1.front () =" <list1.front () <Endl;
Cout <"list1.back () =" <list1.back () <Endl;

// Remove an element from the beginning and end of the list1 Sequence
List1.pop _ Front ();
List1.pop _ back ();
Cout <"list1.pop _ Front () and list1.pop _ back ():" <Endl;
Put_list (list1, "list1 ");

// Clear the 2nd elements in list1
List1.erase (++ list1.begin ());
Cout <"list1.erase (++ list1.begin ():" <Endl;
Put_list (list1, "list1 ");

// Assign a value to list2 and display it
List2.assign (8, 1 );
Cout <"list2.assign (8, 1):" <Endl;
Put_list (list2, "list2 ");

// Display the sequence status information
Cout <"list1.max _ SIZE ():" <list1.max _ SIZE () <Endl;
Cout <"list1.size ():" <list1.size () <Endl;
Cout <"list1.empty ():" <list1.empty () <Endl;

// List sequence container operation
Put_list (list1, "list1 ");
Put_list (list3, "list3 ");
Cout <"list1> list3:" <(list1> list3) <Endl;
Cout <"list1 <list3:" <(list1 <list3) <Endl;

// Sort the list1 container
List1.sort ();
Put_list (list1, "list1 ");

// Combined processing
List1.splice (++ list1.begin (), list3 );
Put_list (list1, "list1 ");
Put_list (list3, "list3 ");
}

Supplement: the STL standard function find is used to search for vector and list linked lists.

# Include <vector>
# Include <algorithm>
# Include <iostream>

Class Example
{
Public:
Example (INT Val)
{
I = val;
}

Bool operator = (example const & RHs)
{
Return (I = RHS. I )? True: false;
}

PRIVATE:
Int I;
};
Using namespace STD;
Int main (void)
{
Vector <example> ve;
Ve. push_back (1 );
Vector <example>: iterator it;
Example ELEM (1 );
It = find (VE. Begin (), VE. End (), ELEM );
Cout <boolalpha <(* It = ELEM );
}

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.