CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2004
    Posts
    22

    remove duplicate elements from a vector

    I'm trying to remove all duplicate elements from a vector<string> like so:

    Code:
    //code to fill vector<string> words;
    std::vector<std::string>::iterator newEnd;
        newEnd = std::unique(words.begin(), words.end());
    	while(newEnd != words.end())
    	{
    		words.erase(words.end());
    	}
    std::endl;
    the size of the vector is different but when I print the contents it still has duplicates. what am i doing wrong?

    edit: sory for posting in wrong forum
    Last edited by kufudo; May 28th, 2004 at 02:29 AM.

  2. #2
    Join Date
    May 2004
    Posts
    22
    never mind, forgot to sort first.

  3. #3
    Join Date
    Jan 2004
    Posts
    44
    Could also just use a set. It stores unique values

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721
    instead of looping like this :

    Code:
    while(newEnd != words.end())
    {
        words.erase(words.end());
    }
    just ...

    Code:
    words.erase(newEnd,words.end());

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured