前言

来源:互联网 发布:手机淘宝如何举报卖家 编辑:程序博客网 时间:2024/05/31 00:40

这篇是从别的博客上面转载的,我觉着在写STL应用方面比较基础,函数涵盖的比较全,所以就搬过来了,另外我会在里面补充一些没有提到的知识

先来一张图片吧!

 

 
容器有序列式容器(Sequence containers)和关联式容器(Associated containers)
序列式容器:每个元素的位置取决于元素被插入的时机,被插入时设置的位置,和元素值本身无关。
序列式容器有vector、deque、list
关联式容器:元素位置取决于特定的排序准则,和插入顺序无关。
关联式容器有set、multiset、map、multimap

 

 

容器经常讲到的一句话: 【 算法 和 数据结构 的分离 】

在学习容器的时候,最好是有一定数据结构基础。

 

 

复制代码
 1 ------------------------------------------------------------ 在半闭区间上应用算法 2 #include <algorithm> 3 #include <iostream> 4 #include <iomanip> 5 using namespace std; 6 int main() 7 { 8     double a[8] = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0}; 9     double val = 3.0;10     double* result = find(a, a+8, val);11     if (result == a+8)12         cout << " 数组没有一个元素的值等于 " << val << endl;13     else14         cout << " 数组有一个元素值等于 " << val << endl;15 //        cout << showpoint << setprecision(2) << " 数组没有一个元素的值等于 " << val << endl;16 //    else17 //        cout << showpoint << setprecision(2) << " 数组有一个元素值等于 " << val << endl;18 19     return 0;20 }
复制代码

 

 

 

复制代码
 1 ------------------------------------------------------------ 函数对象的使用 2 #include <iostream> 3 #include <algorithm> 4 using namespace std; 5 // print 函数对象 6 struct print 7 { 8     void operator() (int x) 9         {cout << x << ' ';}10 };11 12 int main()13 {14     int a[] = {68, 1, 17, 6, 3, 31, 6, 5, 30};15     const int length = sizeof(a) / sizeof(int);16     // 调用函数对象 print, 对每个数组元素进行打印17     for_each(a, a+length, print());18     cout << endl;19 20     return 0;21 }
复制代码

 

 

 

复制代码
 1 /*         解释: 2     reverse_iterator 适配器。 3     程序将 3、6、9 装入 vector 容器后,构造两个 reverse_iterator 反向迭代器,rfirst 指向 vector 尾部,rend 指向 vector头部。 4 通过 rfirst 的 “++” 操作,从尾部迭代到头部,打印遍历的数字,直到 rend位置结束。-------好好思考一下 5 */ 6  7 ------------------------------------------------------------ 反向迭代器 8 #include <iterator> 9 #include <iostream>10 #include <vector>11 using namespace std;12 int main()13 {14     vector<int> vInt;15     vInt.push_back(3);16     vInt.push_back(6);17     vInt.push_back(9);18     reverse_iterator<vector<int>::iterator, int> rfirst(vInt.end());19     reverse_iterator<vector<int>::iterator, int> rend(vInt.begin());20     while (rfirst != rend)21     {22         cout << *rfirst << ' ';23         ++rfirst;24     }25 26     return 0;27 }
复制代码

 

 

 

复制代码
 1 -------------------------------------------- 函数对象适配器的使用 2 #include <vector> 3 #include <iostream> 4 #include <algorithm>  // find_if 算法 5 #include <functional> 6 using namespace std; 7 int main() 8 { 9     vector<int> vInt;10     vInt.push_back(20);11     vInt.push_back(13);12     vInt.push_back(6);13     vInt.push_back(3);14     vInt.push_back(29);15 16     vector<int>::iterator less7_iter = find_if(vInt.begin(), vInt.end(), bind1st(greater<int>(), 7));17     18     cout<< *less7_iter << endl; // 输出结果为 619 20     return 0;21 }
复制代码

 

 

 

 

复制代码
 1 ------------------------------------------------------------- 函数自动转换为函数对象 2 #include <vector> 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; 6 bool less7(int x) 7 { 8     return x < 7; 9 }10 int main()11 {12     vector<int> vInt;13     vInt.push_back(20);14     vInt.push_back(13);15     vInt.push_back(6);16     vInt.push_back(3);17     vInt.push_back(29);18 19     vector<int>::iterator less7_iter = find_if(vInt.begin(), vInt.end(), less7);20     cout << *less7_iter << endl;   // 将打印数字 621 22     return 0;23 }
复制代码
0 0
原创粉丝点击