Algorithm sort Quick Line
#include <algorithm> //Note contains algorithm header file #include<iostream>using namespace Std;int main () { int Arr[] = {1,9,8,4,3,6,0,11}; int length = sizeof (arr)/4; the sort range for quick sort//sort is [start, end], which is used by default from small to large. sort (arr, arr + 3); Sorts only the first 3 sort (arr + 2, arr + length);//Sorts the elements after the 2nd element. Sort (arr, arr + length);//Sorts the entire array sort (arr, arr + length, greater<int> ());//Sort the entire array from large to small return 0;}
Binary_search Two-point search
int arr[] = {0,3,5,7,10,15,19,20,22,24,27};int length = sizeof (arr)/4;bool isfind = True;isfind = Binary_search (arr + 2, Arr + length, 19); Binary search in [Start, end] keycout<< isfind << Endl;
Stack
#include <stack> //note contains the stack header file #include<iostream>using namespace Std;int main () { stack<int > s; Declares the type in the stack, and the stack name s.push (3); S.push (4); into the stack int length = S.size ();//Gets the number of stack elements int top = S.top (); Get stack top element (not out of stack) s.pop (); Out of stack bool isEmpty = S.empty (); return 0;}
Queue
#include <iostream> #include <queue> //note contains the queue header file using namespace Std;int main () { queue< string> Q; string S; Q.push ("Hello"); Queue Q.push ("World"); cout<< Q.front () << Endl; Hello gets the first element value of the team, but not the team cout<< q.back () << Endl; World gets the tail element value, but not the team Q.pop (); Team first element out team cout<< Q.front () << Endl; World cout<< "Size" << q.size () << Endl; cout<< "IsEmpty" << q.empty () <<endl; return 0;}
Set/multiset
Multiset/set uses the data structure of the balanced binary tree, and the insertion and lookup time complexity are log n.
Multiset and set use the same, with only one difference:
1. Duplicate elements can appear in the multiset.
2, the set does not appear duplicate elements, even if the addition of duplicate elements, will automatically go to the weight.
#include <iostream> #include <set>//multiset and set include the set header file using namespace Std;int main () {int arr[10] = {5, 1,2,4,6,4,3,5,8,8};//has a repeating element int i; Multiset<int> MS; Create a Space Multiset collection for (i = 0; i < i++) {Ms.insert (arr[i]); } Multiset<int>::iterator p; Declares an iterator that resembles the pointer for (P = ms.begin (); P! = Ms.end (); p++) {//ms.begin () returns an iterator that points to the first element of Multiset//ms.en D () returns an iterator that points to the next position of the last element of Multiset cout << *p << ""; 1 2 3 4 4 5 5 6 8 8} cout << Endl; int length = Ms.size (); The number of elements in the collection bool IsEmpty = Ms.empty (); Whether the collection is empty int cnt = Ms.count (8); Count occurrences of a number cout<< length << "<< isEmpty <<" "<< cnt << Endl; 10 0 2//Find element, if found, returns an iterator pointing to the found element. If not found, returns the total number of elements in the multiset size p = ms.find (8); if (*p! = Ms.size ()) {cout<< "found" << *p << Endl; 8 Ms.erase (*P); Removes all 8 from the collection, not just one。 cout<< "After delete, the size is" << ms.size () << Endl; } else {cout<< "not found" <<endl; } return 0;}Map/multimap
Both map and Multimap use hash algorithms.
The difference is that the key in the map appears only once, and Multimap can appear many times.
#include <iostream> #include <map> //multimap and map include the set header file using namespace Std;int main () { map <string, string> m; m["one"] = "Hello"; m["n" = "World"; M.insert (pair<string, string> ("Three", "C + +")); BOOL IsEmpty = M.empty (); int length = M.size (); string s = m["one"]; If found, return the corresponding value cout<< s <<endl; Hello s = m["Four"]; If not found, it returns a type 0 value cout<< s <<endl; Returns the empty string map<string, String>::iterator p; p = m.find ("one"); cout<< P->second << Endl; Output one corresponds to the value--Hello m.erase (p); Delete a key for (P = m.begin (); P! = M.end (); p++) { cout<< p->second << ""; C + + World } m.clear ();//clear map return 0;}
C + + implementation algorithm Common STL---collation