• Advertisement
Sign in to follow this  

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

This topic is 2327 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

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.
#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++;
}
};

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement