*/-->
pre.src {background-color: Black; color: White;}
pre.src {background-color: Black; color: White;}
STL unique
unique 删除相邻之间重复的元素
int main(void) { vector<int> v; v.push_back(3); v.push_back(3); v.push_back(3); v.push_back(1); v.push_back(5); sort(v.begin(),v.end()); vector<int>::iterator it = unique(v.begin(), v.end()); v.erase(it, v.end()); int len = v.size(); for (int i = 0; i < len; i++) { printf("%d\n", v[i]); } // 1 3 5 return 0; }
int main(void) { vector<int> v; v.push_back(3); v.push_back(3); v.push_back(3); v.push_back(1); v.push_back(5); sort(v.begin(),v.end()); vector<int> new_v; unique_copy(v.begin(), v.end(), back_inserter(new_v)); int len = new_v.size(); for (int i = 0; i < len; i++) { printf("%d\n", new_v[i]); } // 1 3 5 return 0; }
时间: 2024-10-24 18:50:34