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

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

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

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:
    687
    Patrice
    May 12, 2005
  2. Ben Davies
    Replies:
    3
    Views:
    342
    Erik Max Francis
    Jan 14, 2004
  3. ToshiBoy
    Replies:
    6
    Views:
    824
    ToshiBoy
    Aug 12, 2008
  4. deathweaselx86
    Replies:
    5
    Views:
    1,084
    Raymond Hettinger
    Jun 25, 2011
  5. Token Type
    Replies:
    9
    Views:
    335
    Chris Angelico
    Sep 9, 2012
Loading...

Share This Page