<span style= "font-family:arial, Helvetica, Sans-serif;" > Common stl:</span>
<pre code_snippet_id= "1674595" snippet_file_name= "blog_20160506_1_9718887" name= "code" class= "CPP" ><span ></span>vector: Dynamic array, header file #include <vector>
<span style= "White-space:pre" ></span> definition:vector<type> vec_name;
Define shaping vectorvector<int> a;//add elements dynamically to the end of the container a.push_back (1), A.push_back (2);//Returns the number of elements int n = a.size ();//traversal container for (int i = 0; i<n; i++) {Cout<<a[i]<<endl;} Empty container a.clear ();//string: String container header file # include <string>//definition string str, STR1 = "CD";//Add character element Str.push_back (' a ') to the end; char c = ' B '; str + = c;str + = str1;//output with traversal cout<<str<<endl;int n = str.size (); for (int i=0; i<n; i++) {cout< <str[i]<<endl;} The sub returns a substring of STR with a starting position of Str[_off] and a length of _countstring sub = Str.substr (_off, _count); Str.clear (); Queue: header file #include <queue> queue<type> Q; Queue<int> Q; Q.push (1); Q.push (2); int n = q.size (); while (Q.size () > 0) {cout<<q.front (); Q.pop (); }stack: Stack header file #include <stack>stack<int> s:s.push (1); S.push (2); while (! S.empty ()) {cout<<s.top () <<endl;; S.pop ();} Set: Set set<int> S; S.insert (1); S.insert (2); S.erase (1); int _count = S.count (2); S.clear (); Map: Key-Value Mapping container header file: #include <map>map<int, int> M; M[1] = 1; M[2] = 2;cout<&Lt m[1]<< ' <<M[2]<<endl; M.clear (); Library of common functions: #include <algorithm> #include <cstdlib> #include <math.h> common functions: Sorting functions Sort://int a[100 ]; Vector<int> Vec;sort (A, a+100); sort (Vec.begin (), Vec.end ()), binary lookup function://vector<int> vec//take int container as an example, The IDX returns the subscript of the first element greater than or equal to X in VEC//idx2 returns the subscript of the first greater than x element in VEC, an int idx = Lower_bound (Vec.begin (), Vec.end (), X)-Vec.begin (); int idx2 = Upper_bound (Vec.begin (), Vec.end (), X)-Vec.begin (); Mathematical function: Pow: power function sqrt: square root function ABS: Absolute value function fabs: floating-point absolute value function
2016/05/06