C++学习|数组去重

int uniq_element(int* a, int len) { if(len <= 1) { return len; } hash_set myset; //数据插入hashset for (int i=0; i



int uniq_element(int* a, int len) { if(len <= 1) { return len; } set myset; //数据插入set for (int i=0; i

【C++学习|数组去重】

int uniq_element(int* a, int len) { if(len <= 1) { return len; } //数组存入vector容器 vector myvector (a, a + len); vector::iterator it; //排序 sort(myvector.begin(), myvector.end()); //去重 it = unique (myvector.begin(), myvector.end()); //重置size myvector.resize( it - myvector.begin() ); return myvector.size(); }


int uniq_element(int* a, int len) { if(len <= 1) { return len; } int num = 1; //保存独立元素个数 int i, j; //暴力比较咯 for(i = 1; i < len; i++) { for(j = i - 1; j >= 0; j--) { if(a[j] == a[i]) { break; } else { continue; } } if(-1 == j) { num++; } } return num; }int _tmain(int argc, _TCHAR* argv[]) { int a[] = {1,2,3,1,2,3,3}; int count = uniq_element(a, sizeof(a) / sizeof(int)); cout << count << endl; system("pause"); return 0; }


    推荐阅读