Which is the best container in C++ which can -
- store only unique values (such as
set) - can lookup those values using index in constant time (such as
array)
I basically need to to iterate in phase one and collect all the unique elements, order really doesn't matter.
However, in phase two, I then have to provide each element in the container, but can only provide it one by one. Since caller can know the size of my container, it provides me index one by one, such that 0 < idx < size of the container.
Right now, the only solution that comes to my mind is two maintain two containers vector and set, I am wondering is there any container that provides the same?
class MyContainer{
private:
std::set<Fruits> setFruits;
std::vector<Fruits> arrFruits; // can have indexed access
public:
void collectFruits(const Fruits& fruit){
if(setFruits.find(fruit) == setFruits.end()){
// insert only if it doens't contains
setFruits.insert(fruit);
arrFruits.push_back(fruit);
}
}
};
setalso orders the values. Do you need them to be ordered, or merely that there be no duplicates?