C++ unique

#include <iostream>
#include <algorithm>
#include <list>
#include <iterator>
#include <functional>

using namespace std;


int main()
{
  int source[] = { 1,2,3,3,3,4,5,6,6,7,8,8,8,9,10,3,6,8,12 };
  int sourceNum = sizeof(source)/sizeof(source[0]);

  list<int> list1;
  list<int> list2;
  copy(source,source+sourceNum,back_inserter(list1));
  copy(source,source+sourceNum,back_inserter(list2));

  list<int>::iterator list_iter1;
  for (list_iter1 = list1.begin();list_iter1 != list1.end(); ++list_iter1)
  {
    cout << *list_iter1 << " ";
  }
  cout << endl;

  cout << "----------------------------------------------" << endl;
  list<int>::iterator list_iter2;
  list_iter2 = unique(list1.begin(),list1.end());

  for (list_iter1 = list1.begin();list_iter1 != list_iter2; ++list_iter1)
  {
    cout << *list_iter1 << " ";
  }
  cout << endl;
  cout << "----------------------------------------------" << endl;

  list<int>::iterator list_iter3 = unique(list2.begin(),list2.end(),greater<int>());
  for (list_iter1 = list2.begin(); list_iter1 != list_iter3; ++list_iter1)
  {
    cout << *list_iter1 << " ";
  }
  cout << endl;
  cout << "----------------------------------------------" << endl;


  system("pause");
  return 0;
}

================================================

1 2 3 3 3 4 5 6 6 7 8 8 8 9 10 3 6 8 12
----------------------------------------------
1 2 3 4 5 6 7 8 9 10 3 6 8 12
----------------------------------------------
1 2 3 3 3 4 5 6 6 7 8 8 8 9 10 12
----------------------------------------------
请按任意键继续. . .

posted @ 2019-06-12 19:18  西北逍遥  阅读(367)  评论(0编辑  收藏  举报