STL template class--data structure and algorithm

Source: Internet
Author: User

STL provides some template classes that implement some of the data structure types in the data structure.

When writing code to use the stack, queue and other data structures can make full use of STL template classes, will find particularly useful.

It's a nightmare to think of stacks and queues in the C language with arrays.

C + + is the best language in the world ... (Just kidding!!! )


sequential container : dynamic array vector;deque List of linked lists ;

associative containers :set/multiset ordered values;map/multimap ordered key-value pairs

A. dynamic array vector class

1. Definition: #include <vector> std::vector<int> v;

2. Initialize: V (10); Preset Initial size is 10

V (10,90);//Set 10 x 90

V (v1); Initialize it with another vector class, copy

V (V1.begin (), V1.begin () +5); Copy the first 5 elements of a v1 to it

3. iterator definition: std::vector<int>::iterator p = v.begin;//defines an iterator p; and initializes a point to the beginning of V

4. member functions

(1) insert The element to the end of the array: v.push_back;//Put 50 in the end

Insert element to any position: V (4); v[0]=30;v[1]=29;v[2]=67;v[3]=19;

Insert an element into the Insert function : V.insert (V.begin (), 25);//Insert 25 at the front of V

V.insert (V.end (), 2,25);//Insert 2 25 on the last side of V

V.insert (V.begin+1,v1.begin (), V1.end ())//inserting elements of V1 at V position 1

(2) capacity and size :v.size ();//Returns the current number of elements

v.capacity ();//return capacity, that is, the maximum number of elements can be stored

(3) access element: 1. v[3];//using array subscripts with cross-border risk

2. v.at (3);//Use the AT function to automatically check container size without cross-border risk

3. Vector<int>::iteratorp=v.begin ();  p++; cout<<*p;//iterator Access

(4) Two position distance int i = distance(V.begin (), p)//p is an iterator

(5) Delete the end element: V.pop_back ();

(6) Judgment empty: V.empty (); array null returns 1

two. Dynamic Array deque class :

Similar to vectors, but also supports inserting elements at the beginning

Definition: #include <deque> std::d eque<int> D;

Insert element at the beginning: V.push_front (5);

Delete element at the beginning: V.pop_back ();

three. doubly linked list list

1. definition:#include <list> list<int> l;

2. Insert element:L.push_front (5);//Insert 5 at the beginning of L

L.push_back (5);//Insert 5 at the end of L

L. Insert (p,5);//inserts 5 at iterator p; function returns an iterator that points to the element just inserted

L. Insert (p,2,5);//insertion of 2 5 in position P

L.insert (P,l2.begin (), L2.end ());//Insert a segment of L2 in position 6

3. Delete element : L. Erase (p);//delete the element that the iterator P points to

L.Erase (P1,P2)//Delete an element between iterators P1,P2

L. Clear ();//Clear the entire list

4. Flip The Order of list elements:l.reverse ();

5 reviews sorted by: L.sort ();//ascending order

L.sort (CMP);//ordered by two-dollar predicate CMP

6 list is a linked list structure, can not be used List.begin () + to access the elements, the use of list.begin++ to access

Four Automatic sort Type : Set and the Multiset

1. Definition: #include <set> set<int> s1;multiset<char> s2;//default ascending order

set<int,cmp> s;//using CMP sorting

2. Find S.find (5);//Returns an iterator to 5

3. Delete : S.erase (5);//delete element with value 5

S.erase (P);//delete the element that the iterator P points to

S.erase (P1,P2);//delete elements between P1,P2

Five. Auto Sort type :map and multimap: Store a pair of values

1. definition:#include <map> map<int,char>m1;multimap<int,char> m2// Default keys in ascending order

map<int,char,cmp> m;//using CMP sorting

2. Insert element:M.insert (Make_pair (5,a));//Insert key-value pair (5,a)

3. Find the element ; m.find (5);//Returns an iterator pointing to 5

4. access: Cout<<m->second.c_str (); Convert element value to C string type

5. Delete : M.erase (5);//delete element with value 5

M.erase (P);//delete the element that the iterator P points to

M.erase (P1,P2);//delete elements between P1,P2

Six   self-adapting containers queues queue priority queue priority_queue

stack stack

1. Definition: #include <stack> stack<int> s;//uses deque internally to store data by default

Stack<int,vector<int>> s;//internally uses vectors to store data

2. member function: S.push (5);//top of Stack Insert 5

S.pop ();//delete the top element of the stack

S.empty ();//empty stack returns 1

S.top ();//Get a reference to the top element of the stack cout<<s.top ();

S.size ();//Returns the number of elements in the stack

Queue Queue

1. Definition: #include <queue>queue<int> q;//use deque internally to store data by default

queue<int,list<int>>q;//Internal Use list to store data

2. member function: Q.push (5);//At the end of the queue (i.e. last position) Insert 5

Q.pop ();//delete the first element of the team

Q.front ();//Returns a reference to the first element of the team Cout<<q.front ();

Q.back ();//Returns a reference Cout<<q.back () that points to the tail element of the team;

Q.empty ();//If Empty, return 1

Q.size ();//Returns the number of elements in the queue

Seven. STL general algorithm #include <algorithm>

1. Locate Find Find (V.begin (), V.end (), 5) ,//Find 5 in the iterator interval, return to the iterator, return v.end () if not found

find_if (V.begin (), V.end (), CMP); Check each element in the iterator interval, if the iterator I

Satisfies the CMP (*i) =true, returns the iterator I if not found, then returns V.end ();

boolcmp (int x) {return x%5?0:1;}

2. count Counts (V.begin (), V.end (), 5), //Returns the number of iterator interval 5

count_if (V.begin (), V.end (), CMP); Returns the number of elements that make a CMP function ture

3.v.begin (); Returns the iterator for the first element v.end ();//An iterator that returns the next position of the last element

V.front (); Returns a reference to the first element v.back (); Returns a reference to the last element

4. Sub-sequence search searches (V1.begin (), V1.end (), V2.begin (), V2.end ());//Find sub-sequence V1 in v2, Returns an iterator that points to the first occurrence of a subsequence v2

5. copying copy (V.begin (), V.end (), L.begin ());//copy V to the beginning of L

6. perform operations on each element transform (V.begin (), V.end (), v.begin (), function)//First and second parameter

Number indicates the interval to be manipulated, the third parameter indicates the starting position of the result, and the fourth parameter is the function name

transform (V1.begin (), V1.end (), V2.begin (), v1.begin (), function); The first of the second

Indicates the parameter 1 interval to be manipulated, the third is the start of the parameter 2, and the third is the starting position of the result storage

7 traversal, perform actions on each element of an interval:For_each (V.begin (), v.end (), function)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

STL template class--data structure and algorithm

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.