最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++ list的實(shí)例詳解

 更新時(shí)間:2017年09月29日 09:13:25   作者:陽光島主  
這篇文章主要介紹了 C++ list的實(shí)例詳解的相關(guān)資料,希望通過本文大家能夠理解掌握這部分內(nèi)容,需要的朋友可以參考下

 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

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

相關(guān)文章

最新評(píng)論

察哈| 芦溪县| 曲周县| 津南区| 平顶山市| 台湾省| 饶平县| 名山县| 柯坪县| 怀仁县| 北辰区| 台湾省| 读书| 库伦旗| 新闻| 安仁县| 巨野县| 灌云县| 塔河县| 琼中| 贵定县| 鱼台县| 静安区| 新巴尔虎左旗| 无为县| 五原县| 息烽县| 辽宁省| 长阳| 汤阴县| 区。| 昌邑市| 三江| 洛川县| 洮南市| 康乐县| 自治县| 屏南县| 华坪县| 灌云县| 东平县|