STL sorting (MAP, set, vector, list, stack, queue, deque, priority_queue)

Source: Internet
Author: User
Vector <vector>

Elements stored continuously <vector>

Vector <int> C;

C. Back () returns the last data and does not check whether the data exists.

C. Clear () removes all data from the container.

C. Empty () determines whether the container is empty.

C. Front () returns a data record.

C. pop_back () Delete the last data.

C. push_back (ELEM) adds a data at the end.

C [I] is equivalent to C. at (I );

List <list>

A two-way linked list composed of nodes. Each node contains an element <list>

List <int> list1 (1, 2, 3)

Front () returns the reference of the first element int nret = list1.front () // nret = 1

Back () returns the reference of the last element int nret = list1.back () // nret = 3

Push_back () adds an element to the end of the linked list list1.push _ back (4) // list1 (1, 2, 4)

Push_front () adds an element to the linked list header list1.push _ Front (4) // list1)

Pop_back () deletes an element list1.pop _ back () at the end of the linked list // list1)

Pop_front () deletes an element list1.pop _ Front () in the chain table header // list1 (2, 3)

Clear () delete all elements list1.clear (); // list1 is empty, list1.size () = 0

Sort () sorts linked lists in ascending order by default (customizable callback function)

List object L1 (, 4) l1.sort ();

// L1 (1, 3, 4, 4, 5) l1.sort (greater <int> ());

// L1 (5, 4, 4, 3, 1)

Insert () inserts one or more elements at a specified position.

List1.insert (++ list1.begin (), 9); // list1)

List1.insert (list1.begin (), 2, 9); // list1 (9, 9, 1, 2, 3 );

List1.insert (list1.begin (), list2.begin (), -- list2.end (); // list1 (4, 5, 1, 2, 3 );

Swap () swap two linked lists (two reloads)

List1.swap (list2); // list1 (4, 5, 6) list2 (1, 2, 3)

Unique () deletes adjacent duplicate elements

L1)

L1.unique (); // L1 (, 1)

Merge () merges two ordered linked lists and orders them.

// List1.merge (list2) in ascending order; // list1 (1, 2, 3, 4, 5, 6) list2 is empty

// L1 (, 1), L2 (, 4) l1.merge (L2, greater <int> (); // list1) list2 is empty

Reverse () reverse linked list: list1.reverse (); // list1 (3,2, 1)

Remove () Delete the elements with matching values in the Linked List (all matching elements are deleted) list object L1 (, 4) l1.remove (4); // L1 (, 1 );

Empty () determines whether the linked list is empty bool Bret = l1.empty (); // If L1 is empty, Bret = true; otherwise, Bret = false.

Rbegin () returns the backward pointer (reverse_iteratoror const) List of the last element of the linked list <int >:: reverse_iterator it = list1.rbegin (); // * It = 3

Rend () returns the backward pointer list <int>: reverse_iteratorit = list1.rend (); // * (-- riter) = 1 for the next position of the first element in the linked list.

Set <set>

A red-black tree composed of nodes. Each node contains an element, which is arranged by a certain predicate acting on the element pair. No two different elements can have the same order. <set>

Set <type>: Set with less <> as the sorting rule

Set <type, OP>: Set with OP as the sorting rule

Struct op {

Bool operator () (const rec & A, const rec & B ){

Return A. x <B. x | A. X = B. X & A. Y <B. Y;

}

};

1.1 set::Begin

Function: returns the address of the iterator of the first element.

Set <char>: iterator CP;

Ctr. insert ('A ');

Ctr. insert ('B ');

CP = Ctr. Begin (); // locate the start position of CTR

1.2 set::Clear

Function: delete all elements of a set container.

1.3 set::Count

Function: returns the number of elements corresponding to a keyword. It seems to be 1.

1.4 set::Empty

Function: test whether a set container is empty.

1.5 set::End

Function: return the address of the iterator behind the last element.

1.7 set::Erase

Function: delete one or more elements.

1.8 set::Find

Function: locate the locator of the element equal to the given keyword.

Set <string> CTR;

Ctr. insert ("ABC ");

Ctr. insert ("ABCD ");

Ctr. insert ("abcf ");

Set <string >:: iterator CP;

CP = Ctr. Find ("ABC"); // find the element whose key is 1

If (CP! = Ctr. End ())

Cout <* CP <Endl; // display ABC

CP = Ctr. Find ("ADF"); // find the elements whose key is 2

If (CP! = Ctr. End ())

Cout <* CP <Endl; // not displayed

CP = Ctr. Find ("GFV"); // search for elements whose key is 3

If (CP! = Ctr. End ())

Cout <* CP <Endl; // not displayed

1.10 set::Insert

Function: insert an element or a certain number of elements into a specific position of the set.

1.25 set::Upper_bound

Function: locate the locator whose value points to the first keyword is greater than a given value.

CP = Ctr. upper_bound (2); // minimum element with a larger output ratio than 2

Multi-set <set>

A set of two elements in the same order is allowed. <set>

Multiset <type>: less et with less <> as the sorting rule

Multiset <type, OP>: multise sorted by OP

Struct op {

Bool operator () (const rec & A, const rec & B ){

Return A. x <B. x | A. X = B. X & A. Y <B. Y;

}

};

Multiset <int> h;

_ Typeof (H. Begin () c = H. Begin (); // C points to the address of the first element in the H sequence. The first element is the smallest element.

Printf ("% d", * C); // output the data stored in address c

H. Erase (c); // Delete the elements pointed to by C from the H Sequence

_ Typeof () is a good thing ~

Stack <stack>

<Stack>

Defines the stack variable <int> S;

Stack entry, for example, S. Push (X );

Out stack, for example, S. Pop (); note that the out stack operation only deletes the top element of the stack and does not return this element.

Top access stack, for example, S. Top ()

Judge whether the stack is empty. For example, S. Empty (). If the stack is empty, true is returned.

Number of elements in the access stack, for example, S. Size ()

Queue <queue>

Sorting of first-in-first-out orders <queue>

Define the queue variable <type> m
Check whether the null sample M. Empty () is 1, not 0;
Add M. Push () to the end of an existing element ()
Output the number of existing elements M. Size ()
Display the first element M. Front ()
Show the last element M. Back ()
Clear the first element M. POP ()

Priority_queue <queue>

The element order is determined by a certain predicate acting on the stored value pair. <queue>

1. from large to small by default

Priority_queue <int> qi;

2. You can input a comparison function from small to large output, and use the functional. H function object as the comparison function. Great <int> (small to large) less <int> (large to small)

Priority_queue <int, vector <int>, greater <int> qi2; the second parameter is of the container type. The third parameter is the comparison function.

3. Custom:

Struct CMP // minimum priority queue

{

Bool operator () (const long I, constlong long J)

{

Return I> J;

}

};

Priority_queue <int, vector <Longlong>, CMP> q;

Struct node // minimum priority queue

{

Int ID, Len;

Bool operator <(const node & B) const // only minor signs can be reloaded

{

Return Len> B. Len;

}

};

Priority_queue <node> q;

Q. Empty () // determines whether the queue is empty. If yes, true indicates null. If no value is returned, false indicates empty bool.

Q. Top () // return that the value element of the top element is still in the queue

Q. Pop () // Delete the top element void

Q. Push (v) // Add the long number V to the queue. It will brake the position of condition v void.

Q. Size () // return the number of elements in the queue unsigned int

Deque <deque>

An array consisting of consecutive pointers to different elements <deque>

Deque <int> C

C. pop_back () Delete the last data.

C. pop_front () deletes the header data.

C. push_back (ELEM) adds a data at the end.

C. push_front (ELEM) inserts a data in the header.

C. Clear () removes all data from the container.

C. Front () returns a data record.

C. Back () returns the last data and does not check whether the data exists.

C. Size () returns the actual number of data in the container.

C. Empty () determines whether the container is empty.

C [I] is equivalent to C. at (I );

Map is a set of {key, value} pairs. <map>

Predicate arrangement acting on a key pair <map>

Three data insertion methods (the first and second methods are the same. When map has this keyword, the insert operation cannot insert data, the array will overwrite the value corresponding to the previous keyword)

1. Map <int, string> mapstudent;

Mapstudent. insert (pair <int, string> (1, "student_one "));

Mapstudent. insert (pair <int, string> (2, "student_two "));

2. Map <int, string> mapstudent;

Mapstudent. insert (Map <int, string >:value_type (1, "student_one "));

Mapstudent. insert (Map <int, string >:value_type (2, "student_two "));

3. Map <int, string> mapstudent;

Mapstudent [1] = "student_one ";

Mapstudent [2] = "student_two ";

You can use pair to determine whether the insert is successful.

Map <int, string> mapstudent;

Pair <Map <int, string >:: iterator, bool> insert_pair;

Insert_pair = mapstudent. insert (pair <int, string> (1, "student_one "));

If (insert_pair.second = true)

Cout <"insert successfully" <Endl;

How can I know how much data has already been inserted? The size function can be used as follows: int nsize = mapstudent. Size ();

To clear the data in the map, you can use the clear () function to determine whether there is data in the map. You can use the empty () function.

There are many methods to determine whether a data (keyword) appears in map. Three data search methods are provided here.

First, use the count function to determine whether a keyword exists. The disadvantage is that the data location cannot be located. Due to the characteristics of map, one-to-one ing relationship, it is determined that the return value of the count function has only two values, either 0 or 1. In this case, 1 is returned.

Map <int, string> mapstudent;

Mapstudent. insert (pair <int, string> (1, "student_one "));

Mapstudent. insert (pair <int, string> (2, "student_two "));

Mapstudent. insert (pair <int, string> (3, "student_three "));

Int T1, T2;

T1 = mapstudent. Count (4 );

T2 = mapstudent. Count (1 );

Type 2: Use the find function to locate where the data appears. It returns an iterator. When the data appears, it returns the iterator where the data is located.

Map <string, int> mapstudent;

Mapstudent. insert (pair <string, int> ("student_one", 1 ));

Mapstudent. insert (pair <string, int> ("student_two", 2 ));

Mapstudent. insert (pair <string, int> ("student_three", 3 ));

Map <string, int >:: iteratoriter;

Charch [] = "student_three ";

Iter = mapstudent. Find (CH );

If (ITER! = Mapstudent. End ())

Cout <"find, the value is:" <ITER-> second <Endl;

Else

Cout <"donot find" <Endl;

The clear () function can be used to clear the data in the map to determine whether there is data in the map. The empty () function can be used. If it returns true, it indicates that it is null.

// Use the iterator to delete a file.

Map <int, string >:: iterator ITER;

Iter = mapstudent. Find (1 );

Mapstudent. Erase (ITER );

// If you want to delete it, use the keyword to delete it.

INTN = mapstudent. Erase (1); // if n is deleted, return. Otherwise

// Use an iterator to delete parts

Mapstudent. Erase (mapstudent. Begin (), mapstudent. End ());

// The Code clears the entire map

Mapstudent. Erase (mapstudent. Begin (), mapstudent. End ());

Sort

I,

# Include <map>

# Include <string>

Using namespace STD;

Typedef struct tagstudentinfo

{

Int NID;

String strname;

Booloperator <(tagstudentinfo const & _ A) const

{

// This function specifies the sorting policy, which is sorted by NID. If NID is the same, it is sorted by strname.

If (NID <_ A. NID)

Return true;

If (nid = _ A. NID)

Return strname. Compare (_ A. strname) <0;

Returnfalse;

}

} Studentinfo, * pstudentinfo; // Student Information

Int main ()

{

// Map scores with student information

Map <studentinfo, int> mapstudent;

Studentinfo;

Studentinfo. nid = 1;

Studentinfo. strname = "student_one ";

Mapstudent. insert (pair <studentinfo, int> (studentinfo, 90 ));

Studentinfo. nid = 2;

Studentinfo. strname = "student_two ";

Mapstudent. insert (pair <studentinfo, int> (studentinfo, 80 ));

}

II,

# Include <map>

# Include <string>

# Include <iostream>

Using namespace STD;

Typedef struct tagstudentinfo

{

Int NID;

String strname;

} Studentinfo, * pstudentinfo; // Student Information

Struct sort

{

Bool operator () (studentinfo const & _ A, studentinfo const & _ B) const

{

If (_ A. NID <_ B. NID)

Return true;

If (_ A. nid = _ B. NID)

Return _ A. strname. Compare (_ B. strname) <0;

Return false;

}

};

Int main ()

{

// Map scores with student information

Map <studentinfo, Int, sort> mapstudent;

Studentinfo;

Studentinfo. nid = 1;

Studentinfo. strname = "student_one ";

Mapstudent. insert (pair <studentinfo, int> (studentinfo, 90 ));

Studentinfo. nid = 2;

Studentinfo. strname = "student_two ";

Mapstudent. insert (pair <studentinfo, int> (studentinfo, 80 ));

Map <studentinfo, int >:: reverse_iterator ITER;

For (iter = mapstudent. rbegin (); iter! = Mapstudent. rend (); ITER ++)

Cout <ITER-> second <Endl;

}

Multimap <map>

Allow ing of key pairs in equal order <map>

For example, the same person in the phone book can have more than two phone numbers, and the file system can map multiple symbolic links to the same physical file, or the DNS server can map several URLs to the same IP address.

Search

1. Locate the cursor of the first element of all elements of each key value.

Function: lower_bound (const keytype & X), upper_bound (const keytype & X) you can find the first element of a key value smaller than the specified key value x and the first element of a key value greater than the specified key value x. The returned value is the cursor of the element.

Details: when the maximum value of key value x is reached, upper_bound returns the end cursor of this multimap. Similarly, when the key value x is already the smallest, lower_bound returns the begin cursor of the multimap.

2. Specify a key value for Traversal

You can use the lower_bound and upper_bound functions above for traveling, or use the interval _range function. It returns a cursor pair. The cursor pair: First is the first value of X obtained by the lower_bound function, and the value of the cursor pair: Second is the last value of X obtained by the upper_bound function.

Multimap <int, int>;

A. insert (pair <int, int> (1, 11 ));

A. insert (pair <int, int> (1, 12 ));

A. insert (pair <int, int> (1, 13 ));

A. insert (pair <int, int> (2, 21 ));

A. insert (pair <int, int> (2, 22 ));

A. insert (pair <int, int> (3,31 ));

A. insert (pair <int, int> (3,32 ));

Multimap <int, int >:: iterator p_map;

Pair <multimap <int, int >:: iterator, multimap <int, int >:: iterator> ret;

For (p_map = A. Begin (); p_map! = A. End ();)

{

Cout <p_map-> first <"=> ";

Ret = A. pai_range (p_map-> first );

For (p_map = ret. First; p_map! = Ret. Second; ++ p_map)

Cout <"" <(* p_map). Second;

Cout <Endl;

}

STL sorting (MAP, set, vector, list, stack, queue, deque, priority_queue)

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.