C++ list的實(shí)例詳解
C++ list的實(shí)例詳解
Source:
#include <iostream>
#include <list>
#include <numeric>
#include <algorithm>
using namespace std;
typedef list<int> LISTINT; //創(chuàng)建一個(gè)list容器的實(shí)例LISTINT
typedef list<int> LISTCHAR; //創(chuàng)建一個(gè)list容器的實(shí)例LISTCHAR
int main(void) {
LISTINT listOne; //用LISTINT創(chuàng)建一個(gè)名為listOne的list對(duì)象
LISTINT::iterator i; //聲明i為迭代器
listOne.push_front (2); //從前面向listOne容器中添加數(shù)據(jù)
listOne.push_front (1);
listOne.push_back (3); //從后面向listOne容器中添加數(shù)據(jù)
listOne.push_back (4);
cout<<"listOne.begin()--- listOne.end():"<<endl; //從前向后顯示listOne中的數(shù)據(jù)
for (i = listOne.begin(); i != listOne.end(); ++i)
cout << *i << " ";
cout << endl;
LISTINT::reverse_iterator ir; //從后向后顯示listOne中的數(shù)據(jù)
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創(chuàng)建一個(gè)名為listOne的list對(duì)象
LISTCHAR::iterator j; //聲明j為迭代器
listTwo.push_front ('A'); //從前面向listTwo容器中添加數(shù)據(jù)
listTwo.push_front ('B');
listTwo.push_back ('x'); //從后面向listTwo容器中添加數(shù)據(jù)
listTwo.push_back ('y');
cout<<"listTwo.begin()---listTwo.end():"<<endl; //從前向后顯示listTwo中的數(shù)據(jù)
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=10 listTwo.begin()---listTwo.end(): B A x y The maximum element in listTwo is: y
如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
C++IO流之fstream,?stringstream使用小結(jié)
C語(yǔ)言中常用的輸入輸出函數(shù)有如下幾種:前者是格式化標(biāo)準(zhǔn)輸入輸出,后者是格式化文件輸入輸出,最后是格式化字符串輸入輸出,這篇文章主要介紹了C++IO流:fstream,?stringstream總結(jié),需要的朋友可以參考下2022-04-04
C語(yǔ)言如何使用函數(shù)求素?cái)?shù)和舉例
素?cái)?shù)又稱質(zhì)數(shù),所謂素?cái)?shù)是指除了1和它本身以外,不能被任何整數(shù)整除的數(shù),下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言如何使用函數(shù)求素?cái)?shù)和的相關(guān)資料,需要的朋友可以參考下2022-11-11
Qt串口通信開(kāi)發(fā)之Qt串口通信模塊QSerialPort開(kāi)發(fā)完整實(shí)例(串口助手開(kāi)發(fā))
這篇文章主要介紹了Qt串口通信開(kāi)發(fā)之Qt串口通信模塊QSerialPort開(kāi)發(fā)完整實(shí)例(串口助手開(kāi)發(fā)),需要的朋友可以參考下2020-03-03
C語(yǔ)言中函數(shù)參數(shù)的入棧順序詳解及實(shí)例
這篇文章主要介紹了C語(yǔ)言中函數(shù)參數(shù)的入棧順序詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-02-02
OpenCV實(shí)戰(zhàn)之基于Hu矩實(shí)現(xiàn)輪廓匹配
這篇文章主要介紹了利用C++ OpenCV實(shí)現(xiàn)基于Hu矩的輪廓匹配,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)OpenCV有一定的幫助,感興趣的可以學(xué)習(xí)一下2022-01-01
c語(yǔ)言標(biāo)準(zhǔn)庫(kù)中字符轉(zhuǎn)換函數(shù)和數(shù)字轉(zhuǎn)換函數(shù)
這篇文章主要介紹了c標(biāo)準(zhǔn)庫(kù)中字符轉(zhuǎn)換函數(shù)和數(shù)字轉(zhuǎn)換函數(shù),需要的朋友可以參考下2014-04-04
一篇文章帶你了解C++(STL基礎(chǔ)、Vector)
這篇文章主要為大家詳細(xì)介紹了C++ STL基礎(chǔ),vector向量容器使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能給你帶來(lái)幫助2021-08-08

