#include <iostream>
#include <list>
#include <numeric>
#include <algorithm>
using namespace std;
typedef list< int > LISTINT; //创建一个list容器的实例LISTINT
typedef list< int > LISTCHAR; //创建一个list容器的实例LISTCHAR
int main( void ) {
LISTINT listOne; //用LISTINT创建一个名为listOne的list对象
LISTINT::iterator i; //声明i为迭代器
listOne.push_front (2); //从前面向listOne容器中添加数据
listOne.push_front (1);
listOne.push_back (3); //从后面向listOne容器中添加数据
listOne.push_back (4);
cout<< "listOne.begin()--- listOne.end():" <<endl; //从前向后显示listOne中的数据
for (i = listOne.begin(); i != listOne.end(); ++i)
cout << *i << " " ;
cout << endl;
LISTINT::reverse_iterator ir; //从后向后显示listOne中的数据
cout<< "listOne.rbegin()---listOne.rend():" <<endl;
for (ir =listOne.rbegin(); ir!=listOne.rend();ir++)
cout << *ir << " " ;
cout << endl;
int result = accumulate(listOne.begin(), listOne.end(),0); //使用STL的accumulate(累加)算法
cout<< "Sum=" <<result<<endl;
LISTCHAR listTwo; //用LISTCHAR创建一个名为listOne的list对象
LISTCHAR::iterator j; //声明j为迭代器
listTwo.push_front ( 'A' ); //从前面向listTwo容器中添加数据
listTwo.push_front ( 'B' );
listTwo.push_back ( 'x' ); //从后面向listTwo容器中添加数据
listTwo.push_back ( 'y' );
cout<< "listTwo.begin()---listTwo.end():" <<endl; //从前向后显示listTwo中的数据
for (j = listTwo.begin(); j != listTwo.end(); ++j)
cout << char (*j) << " " ;
cout << endl;
//使用STL的max_element算法求listTwo中的最大元素并显示
j=max_element(listTwo.begin(),listTwo.end());
cout << "The maximum element in listTwo is: " << char (*j)<<endl;
return 0;
}
|