list的部分用法

类别:编程语言 点击:0 评论:0 推荐:
#include <iostream>
#include <algorithm>
#include <list>
#include <iterator>
#include <string>
using namespace std;
template<typename type>
void printlist(const type& elem)
{
 static int c = 0;
 if(c%4 == 0)
  cout << endl;
 cout << elem << "\t";
 c++;
}
int main(void)
{
 list<int> coll_1;
 list<int> coll_2;
 for(int i = 0; i < 20; ++i)
 {
  if(i%2 == 0)
   coll_1.push_back(i);
  else
   coll_2.push_front(i);
 }
 cout << "coll_1: " ;
 for_each(coll_1.begin(), coll_1.end(),
       printlist<int>);
 cout << endl;
 cout << "coll_2:";
 for_each(coll_2.begin(), coll_2.end(),
       printlist<int>);
 cout << endl;
 coll_1.splice(coll_1.begin(), coll_2);
 cout << "coll_1: ";
 for_each(coll_1.begin(), coll_1.end(),
       printlist<int>);
 cout << endl;
 cout << "coll_2:" ;
 for_each(coll_2.begin(), coll_2.end(),
       printlist<int>);
 cout << endl;
 coll_2.assign( find(coll_1.begin(), coll_1.end(), 3),
             coll_1.end() );
 coll_1.sort();
 cout << "coll_1: ";
 for_each(coll_1.begin(), coll_1.end(),
       printlist<int>);
 cout << endl;
 cout << "coll_2:" ;
 for_each(coll_2.begin(), coll_2.end(),
       printlist<int>);
 cout << endl;
 coll_2.sort();
 coll_1.merge(coll_2);
 cout << "coll_1: ";
 for_each(coll_1.begin(), coll_1.end(),
       printlist<int>);
 cout << endl;
 cout << "coll_2:" ;
 for_each(coll_2.begin(), coll_2.end(),
       printlist<int>);
 cout << endl;
 coll_1.unique();
 cout << "coll_1: ";
 for_each(coll_1.begin(), coll_1.end(),
       printlist<int>);
 cout << endl;
 return 0;
}       

本文地址:http://com.8s8s.com/it/it25992.htm