unique list

G

Gary Wessle

Hi

many times I wanted to have a vector<string> of unique "no duplicate"
entries. say I read words from ifstream and don't want repeated words.
is using a map overkill here? since I will not need map.second() part?

thanks
 
V

Victor Bazarov

Gary said:
many times I wanted to have a vector<string> of unique "no duplicate"
entries. say I read words from ifstream and don't want repeated words.
is using a map overkill here? since I will not need map.second() part?

Use 'std::set'. No overkill. Once created, copy the entire set to
a 'vector', if you need fast indexed access. Besides, they are going
to be sorted. Dispose of the 'set' when you don't need it any more.

V
 
M

Mark P

Victor said:
Use 'std::set'. No overkill. Once created, copy the entire set to
a 'vector', if you need fast indexed access. Besides, they are going
to be sorted. Dispose of the 'set' when you don't need it any more.

V

Another possibility is to use a vector, followed by std::sort, followed
by std::unique. If performance is important this may (or may not!)
outperform std::set.
 

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,765
Messages
2,569,568
Members
45,042
Latest member
icassiem

Latest Threads

Top