C++listの実例詳細

2653 ワード

C++listの実例詳細
ソース:

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

 
[[email protected] c++]$ g++ -o list list.cpp 
[[email protected] 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

疑問があれば、メッセージをお願いします。あるいは、当駅のコミュニティで交流して討論してください。ありがとうございます。