以提高程序的全局效率为主,提高局部效率为辅

以提高程序的全局效率为主,提高局部效率为辅。

 1 #include <iostream>
 2 #include <list>
 3 #include <numeric>
 4 #include <algorithm>
 5 
 6 using namespace std;
 7 
 8 //创建一个list容器的实例LISTINT
 9 typedef list<int> LISTINT;
10 
11 //创建一个list容器的实例LISTCHAR
12 typedef list<int> LISTCHAR;
13 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
14 
15 int main(int argc, char** argv) {
16      //--------------------------
17     //用list容器处理整型数据
18     //--------------------------
19     //用LISTINT创建一个名为listOne的list对象
20     LISTINT listOne;
21     //声明i为迭代器
22     LISTINT::iterator i;
23 
24     //从前面向listOne容器中添加数据
25     listOne.push_front (2);
26     listOne.push_front (1);
27 
28     //从后面向listOne容器中添加数据
29     listOne.push_back (3);
30     listOne.push_back (4);
31 
32     //从前向后显示listOne中的数据
33     cout<<"listOne.begin()--- listOne.end():"<<endl;
34     for (i = listOne.begin(); i != listOne.end(); ++i)
35         cout << *i << " ";
36     cout << endl;
37 
38     //从后向后显示listOne中的数据
39     LISTINT::reverse_iterator ir;
40     cout<<"listOne.rbegin()---listOne.rend():"<<endl;
41     for (ir =listOne.rbegin(); ir!=listOne.rend();ir++) {
42         cout << *ir << " ";
43     }
44     cout << endl;
45 
46     //使用STL的accumulate(累加)算法
47     int result = accumulate(listOne.begin(), listOne.end(),0);
48     cout<<"Sum="<<result<<endl;
49     cout<<"------------------"<<endl;
50 
51     //--------------------------
52     //用list容器处理字符型数据
53     //--------------------------
54 
55     //用LISTCHAR创建一个名为listOne的list对象
56     LISTCHAR listTwo;
57     //声明i为迭代器
58     LISTCHAR::iterator j;
59 
60     //从前面向listTwo容器中添加数据
61     listTwo.push_front ('A');
62     listTwo.push_front ('B');
63 
64     //从后面向listTwo容器中添加数据
65     listTwo.push_back ('x');
66     listTwo.push_back ('y');
67 
68     //从前向后显示listTwo中的数据
69     cout<<"listTwo.begin()---listTwo.end():"<<endl;
70     for (j = listTwo.begin(); j != listTwo.end(); ++j)
71         cout << char(*j) << " ";
72     cout << endl;
73 
74     //使用STL的max_element算法求listTwo中的最大元素并显示
75     j=max_element(listTwo.begin(),listTwo.end());
76     cout << "The maximum element in listTwo is: "<<char(*j)<<endl;
77     return 0;
78 }
原文地址:https://www.cnblogs.com/borter/p/9417905.html