標準ライブラリのsortとunique
3271 ワード
ここではsortとuniqueの2つの関数の使い方をテストする簡単なテストコードです.ぼんやりと反復器の影が見えます.
#include
#include
#include
#include
#include
#include
using namespace std;
int main()
{
int arr[] = {1, 3, 0, 3, 5, 7, 9, 4, 5};
int i = 0;
int len = sizeof(arr) / sizeof(int);
vector<int> ivec;
for (i = 0; i < len; i++)
ivec.push_back(arr[i]);
vector<int>::iterator iter;
iter = ivec.begin();
cout << "org data:" << endl;
for (iter = ivec.begin(); iter != ivec.end(); iter++)
cout << *iter << ' ';
cout << endl;
vector<int>::iterator uniq_end;
sort(ivec.begin(), ivec.end());
cout << "sort data:" << endl;
for (iter = ivec.begin(); iter != ivec.end(); iter++)
cout << *iter << ' ';
cout << endl;
uniq_end = unique(ivec.begin(), ivec.end());
ivec.erase(uniq_end, ivec.end());
cout << "uniq data:" << endl;
for (iter = ivec.begin(); iter != ivec.end(); iter++)
cout << *iter << ' ';
cout << endl;
return 0;
}