C++STL容器 vector容器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include <iostream> #include <vector> using namespace std;void printVector (vector<int >& v) { for (vector<int >::iterator it = v.begin (); it != v.end (); it++) cout << *it << " " ; } void test01 () { vector<int >v1; for (int i = 0 ; i < 10 ; i++) { v1.push_back (i); } printVector (v1); vector<int >v2 (v1.begin (), v1.end ()); cout << endl; printVector (v2); cout <<endl; vector<int >v3 (3 , 100 ); printVector (v3); cout << endl; vector<int >v4 (v3); printVector (v4); } int main () { test01 (); return 0 ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 #include <iostream> #include <vector> using namespace std;void printvector (vector<int >&v) { for (vector<int >::iterator it = v.begin (); it != v.end (); it++) { cout << *it << " " ; } cout << endl; } void test01 () { vector<int > v; for (int i = 0 ; i < 10 ; i++) { v.push_back (i); } printvector (v); vector<int >v2; v2 = v; printvector (v2); vector<int >v3; v3 .assign (v.begin (), v.end ()); printvector (v3); vector<int >v4; v4.assign (5 , 100 ); printvector (v4); } int main () { test01 (); return 0 ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include <iostream> #include <vector> using namespace std;void printVector (vector<int >& v) { for (vector<int >::iterator it = v.begin (); it != v.end (); it++) { cout << *it << " " ; } cout << endl; } void test01 () { vector<int >v1; for (int i = 0 ; i < 10 ; i++) { v1.push_back (i); } printVector (v1); if (v1.empty ()) { cout << "v1为空 " << endl; } else { cout << "v1不为空 " << endl; cout << "v1的容量为:" << v1.capacity () << endl; cout << "v1的大小为: " << v1.size () << endl; } v1.resize (15 , 4 ); printVector (v1); v1.resize (5 ); printVector (v1); } int main () { test01 (); return 0 ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 #include <iostream> #include <vector> using namespace std;void printVector (vector<int >&v) { for (vector<int >::iterator it = v.begin (); it != v.end (); it++) { cout << *it << " " ; } cout << endl; } void test01 () { vector<int >v1; v1.push_back (10 ); v1.push_back (20 ); v1.push_back (30 ); v1.push_back (40 ); v1.push_back (50 ); v1.push_back (60 ); printVector (v1); v1.pop_back (); printVector (v1); v1.insert (v1.begin (), 100 ); printVector (v1); v1.insert (v1.begin (), 3 , 500 ); printVector (v1); v1.erase (v1.begin ()); printVector (v1); v1.erase (v1.begin (), v1.begin () + 2 ); printVector (v1); v1.clear (); printVector (v1); } int main () { test01 (); return 0 ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include <iostream> #include <vector> using namespace std;void test01 () { vector<int >v1; for (int i = 0 ; i < 10 ; i++) { v1.push_back (i); } for (int i = 0 ; i < v1.size (); i++) { cout << v1[i] << " " ; } cout << endl; for (int i = 0 ; i < v1.size (); i++) { cout << v1.at (i) << " " ; } cout << endl; cout << "第一个元素为:" << v1.front () << endl; cout << "最后一个元素为:" << v1.back () << endl; } int main () { test01 (); return 0 ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 #include <iostream> #include <vector> using namespace std;void printVector (vector<int >& v) { for (vector<int >::iterator it = v.begin (); it != v.end (); it++) { cout << *it << " " ; } cout << endl; } void test01 () { vector<int >v1; for (int i = 0 ; i < 10 ; i++) { v1.push_back (i); } cout << "交换前的打印:" << endl; printVector (v1); vector<int >v2; for (int i = 10 ; i > 0 ; i--) { v2.push_back (i); } printVector (v2); cout << "交换后的打印:" << endl; v1.swap (v2); printVector (v1); printVector (v2); } void test02 () { vector<int >v; for (int i = 0 ; i < 100000 ; i++) { v.push_back (i); } cout << "v的容量为:" << v.capacity () << endl; cout << "v的大小为:" << v.size () << endl; v.resize (3 ); cout << "v的容量为:" << v.capacity () << endl; cout << "v的大小为:" << v.size () << endl; vector <int >(v).swap (v); cout << "v的容量为:" << v.capacity () << endl; cout << "v的大小为:" << v.size () << endl; } int main () { test01 (); test02 (); return 0 ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include <iostream> #include <vector> using namespace std;void test01 () { vector<int >v1; v1.reserve (100000 ); int num=0 ; int * p = NULL ; for (int i = 0 ; i < 100000 ; i++) { v1.push_back (i); if (p != &v1[0 ]) { p = &v1[0 ]; num++; } } cout << "num = " << num << endl; } int main () { test01 (); return 0 ; }
map容器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include <iostream> #include <map> using namespace std;void printmap (map<int , int >& mp) { for (map<int , int >::iterator it = mp.begin (); it != mp.end (); it++) { cout << "key = " << it->first << " " << "value = " << it->second << endl; } } void test01 () { map<int , int > mp; mp.insert (pair <int , int >(1 , 10 )); mp.insert (pair <int , int >(2 , 20 )); mp.insert (pair <int , int >(3 , 30 )); mp.insert (pair <int , int >(4 , 40 )); mp.insert (pair <int , int >(5 , 50 )); printmap (mp); cout << endl; map<int , int >mp2 (mp); printmap (mp2); cout << endl; map<int , int >mp3; mp3 = mp2; printmap (mp3); } int main () { test01 (); return 0 ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 #include <iostream> #include <map> using namespace std;void printmap (map<int , int >& mp) { for (map<int , int >::iterator it = mp.begin (); it != mp.end (); it++) { cout << "key = " << it->first << " " << "value = " << it->second << endl; } } void test01 () { map<int , int >m; m.insert (pair <int , int >(1 , 10 )); m.insert (pair <int , int >(2 , 20 )); m.insert (pair <int , int >(3 , 30 )); m.insert (pair <int , int >(4 , 40 )); m.insert (pair <int , int >(5 , 50 )); if (m.empty ()) { cout<<"m为空 !" <<endl; } else { cout << "m不为空!" << endl; cout << "m的大小为:" << m.size () << endl; } } void test02 () { map<int , int >m; m.insert (pair <int , int >(1 , 10 )); m.insert (pair <int , int >(2 , 20 )); m.insert (pair <int , int >(3 , 30 )); m.insert (pair <int , int >(4 , 40 )); m.insert (pair <int , int >(5 , 50 )); map<int , int >m2; m2.insert (pair <int , int >(6 , 10 )); m2.insert (pair <int , int >(7 , 20 )); m2.insert (pair <int , int >(8 , 30 )); m2.insert (pair <int , int >(9 , 40 )); m2.insert (pair <int , int >(10 , 50 )); cout << "交换前:" << endl; printmap (m); cout << endl; printmap (m2); cout << "-----------------------" << endl; cout << "交换后:" << endl; m.swap (m2); printmap (m); cout << endl; printmap (m2); } int main () { test02 (); return 0 ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 #include <iostream> #include <map> using namespace std;void printmap (map<int , int >& mp) { for (map<int , int >::iterator it = mp.begin (); it != mp.end (); it++) { cout << "key = " << it->first << " " << "value = " << it->second << endl; } } void test01 () { map<int , int >m; m.insert (pair <int , int >(1 , 10 )); m.insert (make_pair (2 , 20 )); m.insert (map<int , int >::value_type (3 , 30 )); m[4 ] = 40 ; printmap (m); cout << endl; m.erase (m.begin ()); printmap (m); cout << endl; m.erase (3 ); printmap (m); cout << endl; m.erase (m.begin (), m.end ()); printmap (m); cout << endl; m.clear (); } int main () { test01 (); return 0 ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include <iostream> #include <map> using namespace std;void test01 () { map<int , int >m; m.insert (make_pair (1 , 10 )); m.insert (make_pair (2 , 20 )); m.insert (make_pair (3 , 30 )); map<int , int >::iterator pos = m.find (3 ); if (pos != m.end ()) { cout << "找到了元素 key = " << (*pos).first << " " << "value = " << pos->second << endl; } else { cout << "未找到该元素!" << endl; } int num = m.count (3 ); cout << " num = " << num << endl; } int main () { test01 (); return 0 ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include <iostream> #include <map> using namespace std;class myCompare { public : bool operator () (int v1,int v2) const { return v1 > v2; } }; void test01 () { map<int , int ,myCompare>m; m.insert (make_pair (1 , 10 )); m.insert (make_pair (2 , 20 )); m.insert (make_pair (4 , 40 )); m.insert (make_pair (3 , 30 )); m.insert (make_pair (5 , 50 )); m.insert (make_pair (6 , 60 )); for (map<int , int ,myCompare>::iterator it = m.begin (); it != m.end (); it++) { cout << "key = " << it->first << " " << " value = " << it->second << endl; } } int main () { test01 (); return 0 ; }
stack容器 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include <iostream> #include <stack> using namespace std;void test1 () { stack<int >s; s.push (10 ); s.push (20 ); s.push (30 ); s.push (40 ); while (!s.empty ()) { cout << "栈顶元素为:" << s.top () <<" " ; cout << "栈的大小为:" << s.size () << endl; s.pop (); } cout << "栈的大小为:" << s.size () << endl; } int main () { test1 (); return 0 ; }
queue容器 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 #include <iostream> #include <queue> #include <string> using namespace std;class person { public : person (string name, int age) { this ->m_age = age; this ->m_name = name; } string m_name; int m_age; }; void test01 () { queue<person>q; person p1 ("唐僧" , 30 ) ; person p2 ("孙悟空" ,100 ) ; person p3 ("猪八戒" , 200 ) ; person p4 ("沙僧" , 300 ) ; q.push (p1); q.push (p2); q.push (p3); q.push (p4); while (!q.empty ()) { cout << "队头元素-姓名:" << q.front ().m_name << "年龄:" << q.front ().m_age << endl; cout << "队尾元素-姓名:" << q.back ().m_name << "年龄:" << q.back ().m_age << endl; cout << "当前队列中元素个数为:" << q.size () << endl; cout << "-------------------------------------------------------" << endl; q.pop (); } } int main () { test01 (); return 0 ; }
set容器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include <iostream> #include <set> using namespace std;void print_set (set<int >& s) { for (set<int >::iterator it = s.begin (); it != s.end (); it++) { cout << *it << " " ; } cout << endl; } void test01 () { set<int >s1; s1.insert (10 ); s1.insert (20 ); s1.insert (30 ); s1.insert (50 ); s1.insert (60 ); s1.insert (40 ); print_set (s1); set<int >s2 (s1); print_set (s2); set<int >s3; s3 = s2; print_set (s3); } int main () { test01 (); return 0 ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 #include <iostream> #include <set> using namespace std;void print (set<int >& s) { for (set<int >::iterator it = s.begin (); it != s.end (); it++) { cout << *it << " " ; } } void test01 () { set<int >s1; s1.insert (10 ); s1.insert (20 ); s1.insert (30 ); s1.insert (40 ); s1.insert (70 ); s1.insert (50 ); print (s1); if (s1.empty ()) { cout << "s1为空" << endl; } else cout << "s1不为空" << endl; cout << s1.size () << endl; } void test02 () { set<int >s1; s1.insert (10 ); s1.insert (20 ); s1.insert (30 ); s1.insert (40 ); s1.insert (70 ); s1.insert (50 ); set<int >s2; s2.insert (30 ); s2.insert (40 ); s2.insert (50 ); s2.insert (60 ); s2.insert (70 ); s2.insert (90 ); cout << "交换前:" << endl; print (s1); cout << endl; print (s2); cout << endl; cout << endl; cout << "交换后:" << endl; s1.swap (s2); print (s1); cout << endl; print (s2); } int main () { test02 (); return 0 ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 #include <iostream> #include <set> using namespace std;void print (set<int >& s) { for (set<int >::iterator it = s.begin (); it != s.end (); it++) { cout << *it << " " ; } } void test01 () { set<int >s1; s1.insert (10 ); s1.insert (20 ); s1.insert (30 ); s1.insert (40 ); s1.insert (70 ); s1.insert (50 ); print (s1); if (s1.empty ()) { cout << "s1为空" << endl; } else cout << "s1不为空" << endl; cout << s1.size () << endl; } void test02 () { set<int >s1; s1.insert (10 ); s1.insert (20 ); s1.insert (30 ); s1.insert (40 ); s1.insert (70 ); s1.insert (50 ); set<int >s2; s2.insert (30 ); s2.insert (40 ); s2.insert (50 ); s2.insert (60 ); s2.insert (70 ); s2.insert (90 ); cout << "交换前:" << endl; print (s1); cout << endl; print (s2); cout << endl; cout << endl; cout << "交换后:" << endl; s1.swap (s2); print (s1); cout << endl; print (s2); } int main () { test02 (); return 0 ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 #include <iostream> #include <set> using namespace std;void test01 () { set<int >s1; s1.insert (10 ); s1.insert (30 ); s1.insert (69 ); s1.insert (40 ); s1.insert (50 ); set<int >::iterator pos = s1.find (30 ); if (pos != s1.end ()) { cout << "找到元素:" << *pos << endl; } else { cout << "未找到元素" << endl; } } void test02 () { set<int >s1; s1.insert (10 ); s1.insert (30 ); s1.insert (69 ); s1.insert (40 ); s1.insert (50 ); int num = s1.count (30 ); cout << "num=" << num; } int main () { test01 (); test02 (); return 0 ; }
deque容器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include <iostream> #include <deque> using namespace std;void print (const deque<int >& d) { for (deque<int >::const_iterator it = d.begin (); it != d.end (); it++) { cout << *it << " " ; } cout << endl; } void test01 () { deque<int >d1; for (int i = 0 ; i < 10 ; i++) { d1.push_back (i); } print (d1); deque<int >d2 (d1.begin (), d1.end ()); print (d2); deque<int >d3 (10 , 100 ); print (d3); deque<int >d4 (d3); print (d4); } int main () { test01 (); return 0 ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include <iostream> #include <deque> using namespace std;void print (const deque<int >&d) { for (deque<int >::const_iterator it = d.begin (); it != d.end (); it++) { cout << *it << " " ; } cout << endl; } void test01 () { deque<int >d1; for (int i = 0 ; i < 10 ; i++) { d1.push_back (i); } print (d1); deque<int >d2; d2 = d1; print (d2); deque<int >d3; d3.assign (d2.begin (), d2.end ()); print (d3); deque<int >d4; d4.assign (10 , 100 ); print (d4); } int main () { test01 (); return 0 ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include <iostream> #include <deque> using namespace std;void print (const deque<int >&d) { for (deque<int >::const_iterator it = d.begin (); it != d.end (); it++) { cout << *it << " " ; } cout << endl; } void test01 () { deque<int >d1; for (int i = 0 ; i < 10 ; i++)d1.push_back (i); print (d1); if (d1.empty ()) { cout << "d1为空" << endl; } else { cout << "d1不为空" << endl; cout << "d1的大小为:" << d1.size () << endl; } d1.resize (15 ,1 ); print (d1); d1.resize (5 ); print (d1); } int main () { test01 (); return 0 ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 #include <iostream> #include <deque> using namespace std;void print (const deque<int >& d) { for (deque<int >::const_iterator it = d.begin (); it != d.end (); it++) { cout << *it << " " ; } cout << endl; } void test01 () { deque<int >d1; d1.push_back (10 ); d1.push_back (20 ); d1.push_front (5 ); d1.push_front (4 ); print (d1); d1.pop_back (); d1.pop_front (); print (d1); } void test02 () { deque<int >d2; d2.push_back (10 ); d2.push_back (20 ); d2.push_front (5 ); d2.push_front (4 ); print (d2); d2.insert (d2.begin (), 1000 ); print (d2); d2.insert (d2.begin () + 1 , 2 , 500 ); print (d2); deque<int >d3; d3.push_back (1 ); d3.push_back (2 ); d3.push_back (3 ); d3.push_back (4 ); d3.push_back (5 ); d3.insert (d3.begin (), d2.begin (), d2.end ()); print (d3); } void test03 () { deque<int >d1; d1.push_back (10 ); d1.push_back (20 ); d1.push_front (5 ); d1.push_front (4 ); print (d1); d1.erase (d1.begin ()); print (d1); deque<int >::iterator it = d1.begin (); it++; d1.erase (it); print (d1); d1.erase (d1.begin (), d1.end ()); d1.clear (); print (d1); } int main () { test01 (); test02 (); test03 (); return 0 ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include <iostream> #include <deque> using namespace std;void test01 () { deque<int >d1; for (int i = 0 ; i < 10 ; i++) { d1.push_back (i); } for (int i = 0 ; i < d1.size (); i++) { cout << d1[i] << " " ; } cout << endl; for (int i = 0 ; i < d1.size (); i++) { cout << d1.at (i) << " " ; } cout << endl; cout << d1.front () << " " << d1.back () << endl; } int main () { test01 (); return 0 ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include <iostream> #include <deque> #include <algorithm> using namespace std;void print (const deque<int >& d) { for (deque<int >::const_iterator it = d.begin (); it != d.end (); it++) { cout << *it << " " ; } cout << endl; } void test01 () { deque<int >d1; d1.push_back (20 ); d1.push_back (50 ); d1.push_back (40 ); d1.push_back (70 ); d1.push_front (30 ); d1.push_front (60 ); d1.push_front (100 ); cout << "排序前的容器:" << endl; print (d1); cout << "排序后的容器:" << endl; sort (d1.begin (), d1.end ()); print (d1); } int main () { test01 (); return 0 ; }
priority_queue容器 优先队列,其底层是基于堆来进行实现的,在优先队列中,队首元素一定是当前队列中优先级最高的那一个
1.priority_queue的定义
1 priority_queue<typename > name;
2.priority_queue容器内元素的访问
只能通过top()函数来访问队首元素(堆顶元素),时间复杂度为O(1),也就是优先级最高的元素,通过push()函数入队,其时间复杂度为O(logN),通过pop()函数弹出队首元素出队,时间复杂度为O(logN)。empty()函数判断优先队列是否为空,返回true为空,返回false为非空。size()函数返回优先队列内元素的个数,时间复杂度为O(1);
3.priority_queue内元素优先级的设置
(1)基本数据类型的优先级设置
1 2 3 priority_queue<int >q; priority_queue<int ,vector<int >, less<int > >q; priority_queue<int ,vector<int >, greater<int > >q;
第二个参数vector参数填写的是承载底层数据结构堆(heap)的容器,第三个参数less则是对第一个参数的比较类,less表示数字大的优先级大,greater表示数字小的优先级大。
(2)结构体的优先级设置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include <iostream> #include <cmath> #include <queue> using namespace std;struct fruit { string name; int price; }f1, f2, f3; struct cmp { bool operator () (const fruit& a,const fruit& b) { return a.price < b.price; } }; int main () { priority_queue<fruit, vector<fruit>, cmp> q; f1.name = "桃子" ; f1.price = 15 ; f2.name = "苹果" ; f2.price = 20 ; f3.name = "菠萝" ; f3.price = 12 ; q.push (f1); q.push (f2); q.push (f3); cout << q.top ().name << " " << q.top ().price << endl; return 0 ; }
pair容器 pair实际上可以看作一个内部含有两个元素的结构体
1 2 3 4 struct pair { typename first; typename second; };
1.pair的定义,要使用pair要添加头文件#include,由于map的内部涉及pair,故添加map头文件也能使用pair.
1 pair<typename1, typename2> name;
2.pair中元素的访问
pair中只有两个元素,分别是frist和second,只需要按照正常结构体方式去访问即可。
3.pair常用函数
比较操作数
两个pair类型数据进行比较可以直接使用==,!=,>,<比较大小,比较规则是先以first的大小作为标准,只有当first相等时才会去判别second的大小。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <iostream> #include <utility> using namespace std;int main () { pair<int ,int >p1 (5 ,10 ); pair<int ,int >p2 (5 ,22 ); pair<int ,int >p3 (3 ,20 ); cout<<(p1<p2?"p1<p2" :"p1>=p2" )<<endl; cout<<(p1<p3?"p1<p3" :"p1>=p3" )<<endl; return 0 ; }
algorithm头文件下的常用函数 1.max(),min(),abs()
max(x,y)和min(x,y)分别返回x与y中的最大值与最小值,且参数必须是两个(可以是浮点数),abs(x)返回x的绝对值。且x必须是整数,浮点数的绝对值采用fabs()。
2.swap()
swap(x,y)用来交换x与y的值。
3.reverse(it,it2)可以将数组指针在[it,it2)之间的元素或者容器的迭代器在[it,it2)范围内的元素进行反转。
4.next_permutation()
next_permutation()给出一个序列在全序列中的下一个序列。
1 2 3 4 5 6 int arr[4 ]={1 ,2 ,3 ,4 };do { cout<<arr[0 ]<<arr[1 ]<<arr[2 ]<<arr[3 ]<<endl; }while (next_permutation (arr,arr+4 ));
5.fill()
fill()可以把数组或者容器中的某一段区间赋值某个相同的值。和memset不同,这里的赋值可以是数组类型对应范围中的任意值。
6.sort()
sort(首元素地址,尾元素地址的下一个地址,比较函数)对数据进行排序
7.lower_bound()和upper_bound()
lower_bound和upper_bound需要用在一个有序数组或者容器中
lower_bound(first,last,val)用来寻找在数组或者容器的[first,last)范围内第一个值大于等于val的元素的位置
upper_bound(first,last,val)用来寻找在数组或者容器的[first,last)范围内第一个值大于val的元素的位置
1 2 3 4 5 int arr[10 ]={1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 };int *lowerPos=lower_bound (arr,arr+10 ,2 );int *upperPos=upper_bound (arr,arr+10 ,2 );cout<<"第一个大于等于2的元素的位置为:" <<lowerPos<<endl; cout<<"第一个大于2的元素的位置为:" <<upperPos<<endl;