Sign in to follow this  
thedodgeruk

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

Recommended Posts

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 ?

Share this post


Link to post
Share on other sites
...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.

Share this post


Link to post
Share on other sites
std::map<T *, int> or std::unordered_map<T*, int> can easily be used for that purpose.

EDIT: Some code for you.
[code]#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++;
}
};
[/code]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this