-
May 28th, 2004, 02:19 AM
#1
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.
-
May 28th, 2004, 02:43 AM
#2
never mind, forgot to sort first.
-
May 28th, 2004, 03:50 AM
#3
Could also just use a set. It stores unique values
-
May 28th, 2004, 05:54 AM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|