STL学习笔记之前篇

来源:互联网 发布:穆逢春 知乎 编辑:程序博客网 时间:2024/05/21 17:51

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

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

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

 ------------------------------------------------------------ 在半闭区间上应用算法 #include <algorithm> #include <iostream> #include <iomanip> using namespace std; int main() {     double a[8] = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0};     double val = 3.0;     double* result = find(a, a+8, val);     if (result == a+8)         cout << " 数组没有一个元素的值等于 " << val << endl;     else         cout << " 数组有一个元素值等于 " << val << endl; //      cout << showpoint << setprecision(2) << " 数组没有一个元素的值等于 " << val << endl; //    else //        cout << showpoint << setprecision(2) << " 数组有一个元素值等于 " << val << endl;     return 0; }

  ------------------------------------------------------------ 函数对象的使用 #include <iostream> #include <algorithm> using namespace std; // print 函数对象 struct print {     void operator() (int x)         {cout << x << ' ';} }; int main() {     int a[] = {68, 1, 17, 6, 3, 31, 6, 5, 30};     const int length = sizeof(a) / sizeof(int);     // 调用函数对象 print, 对每个数组元素进行打印     for_each(a, a+length, print());     cout << endl;     return 0; }


 /*         解释:     reverse_iterator 适配器。     程序将 3、6、9 装入 vector 容器后,构造两个 reverse_iterator 反向迭代器,rfirst 指向 vector 尾部,rend 指向 vector头部。 通过 rfirst 的 “++” 操作,从尾部迭代到头部,打印遍历的数字,直到 rend位置结束。-------好好思考一下 */ ------------------------------------------------------------ 反向迭代器 #include <iterator> #include <iostream> #include <vector> using namespace std; int main() {     vector<int> vInt;     vInt.push_back(3);     vInt.push_back(6);     vInt.push_back(9);     reverse_iterator<vector<int>::iterator, int> rfirst(vInt.end());     reverse_iterator<vector<int>::iterator, int> rend(vInt.begin());     while (rfirst != rend)     {         cout << *rfirst << ' ';         ++rfirst;     }     return 0; }
 -------------------------------------------- 函数对象适配器的使用 #include <vector> #include <iostream> #include <algorithm>  // find_if 算法 #include <functional> using namespace std; int main() {     vector<int> vInt;     vInt.push_back(20);     vInt.push_back(13);     vInt.push_back(6);     vInt.push_back(3);     vInt.push_back(29);      vector<int>::iterator less7_iter = find_if(vInt.begin(), vInt.end(), bind1st(greater<int>(), 7));          cout<< *less7_iter << endl; // 输出结果为 6     return 0; }
 ------------------------------------------------------------- 函数自动转换为函数对象 #include <vector> #include <iostream> #include <algorithm> using namespace std; bool less7(int x) {     return x < 7; } int main() {     vector<int> vInt;     vInt.push_back(20);     vInt.push_back(13);     vInt.push_back(6);     vInt.push_back(3);     vInt.push_back(29);     vector<int>::iterator less7_iter = find_if(vInt.begin(), vInt.end(), less7);     cout << *less7_iter << endl;   // 将打印数字 6     return 0; }

------------------------------最好还是找一本适合自己水平的STL方面的书籍来阅读,网上有很多这样的资料。例如 百度文库,新浪爱问知识人,51CTO,CSDN论坛,大家网论坛,等等。。。。

  以上内容,仅供自己 复习、参考 之用。   有不正确的地方,希望各位读者,能批评指正,谢谢,我也是来学习的。


原文链接:http://www.cnblogs.com/music-liang/archive/2013/04/03/2998061.html

0 0
原创粉丝点击