i need to be able to push pointers onto a container
the container can only allow one of each pointer address.
and when add the pointer , i need the container to return an int for its index
is there such a container , its for a look up table ?
need to find the correct stl for this , or is there one
Do you need an int for the index? If you don't mind a iterator or reference you can use std::set or unordered_set.
...though keep in mind std::unique() only removes consecutive duplicates. You may need to sort the vector before calling std::unique(), and that may reorder elements rendering previous indices invalid.
std::map<T *, int> or std::unordered_map<T*, int> can easily be used for that purpose.
EDIT: Some code for you.
EDIT: Some code for you.
#include <unordered_map>
template <typename T>
class Enumeration {
std::unordered_map<T, int> map;
int size;
public:
Enumeration() : size(0) {
}
int add(T const &t) {
typename std::unordered_map<T, int>::iterator it = map.find(t);
if (it != map.end())
return it->second;
map.insert(std::pair<T, int>(t, size));
return size++;
}
};
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement