stl container that store unique items

M

marcwentink

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

deane_gavin

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
 
K

Kai-Uwe Bux

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
 
N

Neelesh Bodas

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)
 
D

deane_gavin

Neelesh said:
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
 
M

marcwentink

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top