unique list

Discussion in 'C++' started by Gary Wessle, Feb 9, 2007.

  1. Gary Wessle

    Gary Wessle Guest

    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
     
    Gary Wessle, Feb 9, 2007
    #1
    1. Advertisements

  2. Gary Wessle wrote:
    > 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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Feb 9, 2007
    #2
    1. Advertisements

  3. Gary Wessle

    Mark P Guest

    Victor Bazarov wrote:
    > Gary Wessle wrote:
    >> 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


    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.
     
    Mark P, Feb 9, 2007
    #3
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    938
    Patrice
    May 12, 2005
  2. Ben Davies
    Replies:
    3
    Views:
    415
    Erik Max Francis
    Jan 14, 2004
  3. Roy Smith

    Building unique comma-delimited list?

    Roy Smith, Jan 5, 2005, in forum: Python
    Replies:
    5
    Views:
    620
    Bengt Richter
    Jan 6, 2005
  4. Delaney, Timothy C (Timothy)

    RE: Best way to make a list unique?

    Delaney, Timothy C (Timothy), Mar 8, 2005, in forum: Python
    Replies:
    3
    Views:
    389
    Kent Johnson
    Mar 9, 2005
  5. Delaney, Timothy C (Timothy)

    RE: Best way to make a list unique?

    Delaney, Timothy C (Timothy), Mar 9, 2005, in forum: Python
    Replies:
    2
    Views:
    411
    Paul Rubin
    Mar 12, 2005
  6. ToshiBoy
    Replies:
    6
    Views:
    1,107
    ToshiBoy
    Aug 12, 2008
  7. deathweaselx86
    Replies:
    5
    Views:
    1,588
    Raymond Hettinger
    Jun 25, 2011
  8. Token Type
    Replies:
    9
    Views:
    555
    Chris Angelico
    Sep 9, 2012
Loading...