Thread: uniue algorithm

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    104

    unique algorithm

    I have three vectors.
    I want to remove duplicates within each vector.
    I've tried using
    Code:
    vector.erase(uniqe(vector.begin(),vector.end())
    but that removes elements not unique to all the vectors.
    Am I not using this algorithm correctly or is there another way
    to accomplish this?
    Last edited by kes103; 03-27-2003 at 01:36 PM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    To remove duplicate elements within a vector, you must first ensure that the elements within the vector are sorted. The unique function returns an iterator representing the new logical end of the container. The erase member function for vectors must then be called to actually remove the elements from this logical end position to the real end position. This is close to what you had but there were a couple things you missed.
    Code:
    #include <algorithm>
    #include <vector>
    using namespace std;
    ...
    vector<int> IntVector;
    ...
    sort( IntVector.begin(), IntVector.end() ); // Make sure vector is sorted first
    IntVector.erase( unique(IntVector.begin(),IntVector.end()), IntVector.end() );
    You mispelled 'unique' in your implementation (a typo?) and forgot the second argument to the erase function but that is it. This should work.
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implement of a Fast Time Series Evaluation Algorithm
    By BiGreat in forum C Programming
    Replies: 7
    Last Post: 12-04-2007, 02:30 AM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM