编程语言

img owl2008

list的部分用法

发表于2004/10/31 15:28:00  1202人阅读

分类: boost/STL

#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;
}       
阅读全文
0 0

相关文章推荐

img
取 消
img