need to find the correct stl for this , or is there one

Started by
3 comments, last by alvaro 12 years, 6 months ago
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 ?
Advertisement
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.
If you really need an int for an index, std::vector and std::unique are your best friends.

Stephen M. Webb
Professional Free Software Developer

...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.
#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