表示set不能随机存取,所以只好换vector了,但存取结束之后,必须删除重复元素:
/*
(C) OOMusou 2007 http://oomusou.cnblogs.com
Filename : GenericAlgo_eliminate_duplicate.cpp
Compiler : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
Description : Demo how to duplicated element in container
Release : 05/27/2007 1.0
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
template<typename T>
inline void eliminate_duplicates(T& c) {
sort(c.begin(), c.end());
c.erase(unique(c.begin(), c.end()), c.end());
}
int main() {
int ia[] = {1, 1, 2, 3, 3};
vector<int> ivec(ia, ia + sizeof(ia) / sizeof(int));
eliminate_duplicates(ivec);
copy(ivec.begin(), ivec.end(), ostream_iterator<int>(cout, " "));
}
ok,就这样了