Thread: container question

  1. #1
    Registered User
    Join Date
    May 2006

    container question


    Is there any c++ container that will only allow/keep unique elements.

    I need a vector of strings, which should be all unique (no duplicates).

    What would be the best way to solve this?

    Many thanks in advance!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    The set container would only allow unique values to be stored, a set<string> container for example would not allow two copies of the word "apple" to be inserted however things are case sensitive so "apple" and "Apple" would be considered different and therefore both would be allowed into the container.

    That is as a default of course (which uses the default operator< as a comparison object), you could use the optional second parameter to the instantiation of the set container such that it uses your own custom comparison function that would (for instance) disregard case when comparing objects and therefore not allow "Apple" if "apple" had already existed within the container.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jan 2005
    The set is probably your best bet, but depending on your situation you might also just add all your data to a vector, then use algorithms to remove non-unique elements. This might make sense if you don't have many duplicates and you add all your data to the container once at the beginning before you need duplicates removed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about erase()
    By Sharke in forum C++ Programming
    Replies: 8
    Last Post: 06-10-2009, 01:22 AM
  2. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  3. Linked List Queue Implementation help
    By Kenogu Labz in forum C++ Programming
    Replies: 8
    Last Post: 09-21-2005, 10:14 AM
  4. Question about document/view app in MFC
    By hpy_gilmore8 in forum Windows Programming
    Replies: 2
    Last Post: 05-06-2004, 07:51 PM
  5. Question...
    By TechWins in forum A Brief History of
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM