C++ unique

时间:2019-06-12
本文章向大家介绍C++ unique,主要包括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
----------------------------------------------
请按任意键继续. . .

$flag 上一页 下一页