"David Loo" wrote:
I want to use a map to store a list of strings and i dont want a duplicate
string added to the list. how can I go about this using the STL map?
a std::map stores pairs of values, ensuring the first element of each stored
pair is unique.
so you can for example do something like:
class unique_strings: public map<string,int> // stores strings without
duplicates, int is dummy
{
typedef pair<string,int> value_type; // normally already defined in
map
// constructors...
bool insert(const string& s) {return insert(value_type(s,1)).parent;}
// map::insert returns a pair<iterator, bool> result
bool contains(const string& s) const {return find(s)!=end();} //
map::find returns an iterator to the found pair
};
Note that you can very easily use the <int> term of the pair to store a
number of occurences...
--
Philippe Guglielmetti -
www.dynabits.com