STL学习笔记--前篇

  终于,我又开始更新博客了。。。之所以没更新,是因为没学到什么知识,马上就毕业了,必须【闭关】学点东西啊。

  以前 ,总觉得 STL 这类东西,等毕业了在去学还来得及。。。但是,笔试了几家公司之后,觉得很多公司都看重这方面的知识, STL 应该在【毕业之前】就学习。

  还有,就是觉得 STL 好抽象,不知道从哪开始。 

  大家都一样的,面对未知的东西,在学习或者操作之前,我们总是会感到迷茫,这很正常。。只要能坚持,能不断去寻找适合的学习的资料、学习的方法,我们总会有突破的。。。哎。这个,只能靠自己了。多看书,敲代码吧。。我也正在这样进行中。。。  在学习 STL 之前,总得有点模板的概念吧。。。很多C++编程书籍里面都讲到了。可以翻到相应的章节,看一看。

  这是 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     else
14         cout << " 数组有一个元素值等于 " << val << endl;
15 //        cout << showpoint << setprecision(2) << " 数组没有一个元素的值等于 " << val << endl;
16 //    else
17 //        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; // 输出结果为 6
19 
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;   // 将打印数字 6
21 
22     return 0;
23 }

 

 

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

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

 

 

 

posted @ 2013-04-03 15:39  He_LiangLiang  阅读(685)  评论(0编辑  收藏  举报