By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,434 Members | 1,878 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,434 IT Pros & Developers. It's quick & easy.

stl container that store unique items

P: n/a
Could you please advise me what C++ STL container I could use that
would automaticly only store unique items. Hence prevents the insertion
of doubles.

It should behave like this, (I use 'vector' but I think this does not
behave this way)

vector<string> container;

container.push("1234");
container.push("2234");
container.push("3234");
container.push("4234");
container.push("1234");

Now the container should only contain, the four unique strings: "1234"
"2234" "3234" "4234" and no double item for "1234".

I am reading some stuff here and there, but I cannot find the answer to
this simple question....

Nov 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

marcwent...@hotmail.com wrote:
Could you please advise me what C++ STL container I could use that
would automaticly only store unique items. Hence prevents the insertion
of doubles.

It should behave like this, (I use 'vector' but I think this does not
behave this way)

vector<string> container;

container.push("1234");
container.push("2234");
container.push("3234");
container.push("4234");
container.push("1234");

Now the container should only contain, the four unique strings: "1234"
"2234" "3234" "4234" and no double item for "1234".

I am reading some stuff here and there, but I cannot find the answer to
this simple question....


std::set

Gavin Deane

Nov 22 '05 #2

P: n/a
> Now the container should only contain, the four unique strings: "1234"
"2234" "3234" "4234" and no double item for "1234".


stl::set could be an option, provided that ordering of your unique
values is not important. In other words, if you still _do care_ about
the "relative position of the elements" inside the container (eg 6 is
inserted before 1 so 6 must occur before 1 etc etc) , then stl::set is
not a correct choice.
(Of course, in this specific example it makes no difference)

Nov 22 '05 #3

P: n/a

Neelesh Bodas wrote:
stl::set could be an option, provided that ordering of your unique


typo. std::set.

Nov 22 '05 #4

P: n/a
ma*********@hotmail.com wrote:
Could you please advise me what C++ STL container I could use that
would automaticly only store unique items. Hence prevents the insertion
of doubles.

It should behave like this, (I use 'vector' but I think this does not
behave this way)

vector<string> container;

container.push("1234");
container.push("2234");
container.push("3234");
container.push("4234");
container.push("1234");

Now the container should only contain, the four unique strings: "1234"
"2234" "3234" "4234" and no double item for "1234".

I am reading some stuff here and there, but I cannot find the answer to
this simple question....


Check out std::set<>.
Best

Kai-Uwe Bux
Nov 22 '05 #5

P: n/a

Neelesh Bodas wrote:
Now the container should only contain, the four unique strings: "1234"
"2234" "3234" "4234" and no double item for "1234".


stl::set could be an option, provided that ordering of your unique
values is not important. In other words, if you still _do care_ about
the "relative position of the elements" inside the container (eg 6 is
inserted before 1 so 6 must occur before 1 etc etc) , then stl::set is
not a correct choice.
(Of course, in this specific example it makes no difference)


To be more specific, one of the types used to parameterise the std::set
template is a comparison type used to determine the order in which
elements are stored. If you don't specifiy your own type for this, it
defaults to a less-than comparison. So for a std::set<int>, if you
insert 6 and 1, the 1 will always be stored before the 6, whichever one
you happened to insert first.

So yes, this is a property of std::set that may make it inappropriate
for some uses.

Gavin Deane

Nov 22 '05 #6

P: n/a
No, the order is not important, I just want to extract a list of
identifier strings and use this in another part of the program. I get
these Id's from a table in which this string is a foreign key, not the
primary key.

So thanks a lot!!

Nov 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.