国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++ list的實例詳解

2020-05-23 13:43:11
字體:
來源:轉載
供稿:網友

 C++ list的實例詳解

Source:

#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; }    

Result:

 [work@db-testing-com06-vm3.db01.baidu.com c++]$ g++ -o list list.cpp [work@db-testing-com06-vm3.db01.baidu.com c++]$ ./list listOne.begin()--- listOne.end():1 2 3 4 listOne.rbegin()---listOne.rend():4 3 2 1 Sum=10listTwo.begin()---listTwo.end():B A x y The maximum element in listTwo is: y

如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 松原市| 靖边县| 卢氏县| 博乐市| 神池县| 华安县| 漳州市| 澎湖县| 隆子县| 集贤县| 芦山县| 龙江县| 本溪| 上犹县| 长武县| 鄯善县| 小金县| 特克斯县| 绥棱县| 翼城县| 油尖旺区| 白玉县| 社会| 项城市| 宁海县| 和林格尔县| 兖州市| 永靖县| 余庆县| 正镶白旗| 扶风县| 潞城市| 肇州县| 内江市| 兴隆县| 军事| 克东县| 赫章县| 牡丹江市| 江津市| 山丹县|