Remove dublicates from std::vector<string>

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2008
Posts: 517
Reputation: Jennifer84 is an unknown quantity at this point 
Solved Threads: 1
Jennifer84 Jennifer84 is offline Offline
Posting Pro

Remove dublicates from std::vector<string>

 
0
  #1
Mar 10th, 2008
I have a vector that contains these elements( std::vector<string> Values(6); )
What I wonder here is how it is possible to remove "dublicates" from Values(6) ?
As in this example there is 2 dublicates that are exactly the same.

5,1536
5,1537
5,1538
5,1537 //This is a dublicate
5,1538 //This is a dublicate
5,1539
Last edited by Jennifer84; Mar 10th, 2008 at 5:33 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 34
Reputation: cbattagler is an unknown quantity at this point 
Solved Threads: 4
cbattagler cbattagler is offline Offline
Light Poster

Re: Remove dublicates from std::vector<string>

 
0
  #2
Mar 10th, 2008
Easiest way to do it will be to #include algorithm and sort the vector, and then use unique

  1. std::sort(Values.begin(), Values.end());
  2. vector<Lidar>::iterator new_end = std::unique(Values.begin(), Values.end());

What that does is move the duplicate entries to the end of the vector and give you a new iterator showing where the new ending is.

Also, it is duplicates.

Cameron
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,660
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 723
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Remove dublicates from std::vector<string>

 
0
  #3
Mar 10th, 2008
>how it is possible to remove "dublicates" from Values(6) ?
I'd start by sorting the vector and calling std::unique on it. Of course, that's mentally translating "dublicate" into "duplicate". If you really do mean "dublicate", I have no idea what you're talking about.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 517
Reputation: Jennifer84 is an unknown quantity at this point 
Solved Threads: 1
Jennifer84 Jennifer84 is offline Offline
Posting Pro

Re: Remove dublicates from std::vector<string>

 
0
  #4
Mar 10th, 2008
Sorry, my mistake. I do meen "Duplicates". So I am trying to take all "Duplicates" away from the std::vector<string>.

I have tried to put this code and also I have #include <algorithm>
When I compile this. The compiler says that Lidar is a "undeclared identifier".
I have never heard about Lidar and what it could be...
Thanks.
  1. std::sort(Values.begin(), Values.end());
  2. vector<Lidar>::iterator new_end = std::unique(Values.begin(), Values.end());


Originally Posted by Narue View Post
>how it is possible to remove "dublicates" from Values(6) ?
I'd start by sorting the vector and calling std::unique on it. Of course, that's mentally translating "dublicate" into "duplicate". If you really do mean "dublicate", I have no idea what you're talking about.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 34
Reputation: cbattagler is an unknown quantity at this point 
Solved Threads: 4
cbattagler cbattagler is offline Offline
Light Poster

Re: Remove dublicates from std::vector<string>

 
0
  #5
Mar 10th, 2008
Oops, that was my fault, just cut and pasted some code I was working on change Lidar to string in your case..

Cameron
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 517
Reputation: Jennifer84 is an unknown quantity at this point 
Solved Threads: 1
Jennifer84 Jennifer84 is offline Offline
Posting Pro

Re: Remove dublicates from std::vector<string>

 
0
  #6
Mar 10th, 2008
Yes okay... thank you. I did just wonder a bit there

When have written this, I am not sure exactly what has happend here:
  1. std::sort(ReadInData.begin(), ReadInData.end());
  2. vector<string>::iterator new_end = std::unique(ReadInData.begin(), ReadInData.end());

For the code below, I am trying to put the "Removed duplicates vector" into a outFile. So I beleive I have to iterate through the vectors begin to the end.
I am not sure what is the end now because I think the "duplicates" is altogether in the end of the vector.
So it should be the for () I am a little unsure how to do.
  1. for (std::vector<string>::iterator new_end)
  2. {
  3. outFile << ReadInData[].c_str() << '\n';
  4. }

Thank you...

Originally Posted by cbattagler View Post
Oops, that was my fault, just cut and pasted some code I was working on change Lidar to string in your case..

Cameron
Last edited by Jennifer84; Mar 10th, 2008 at 6:22 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 34
Reputation: cbattagler is an unknown quantity at this point 
Solved Threads: 4
cbattagler cbattagler is offline Offline
Light Poster

Re: Remove dublicates from std::vector<string>

 
0
  #7
Mar 10th, 2008
This is my favorite article about using iterators, check it out http://www.oreillynet.com/pub/a/netw...us.html?page=2

Cameron
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 517
Reputation: Jennifer84 is an unknown quantity at this point 
Solved Threads: 1
Jennifer84 Jennifer84 is offline Offline
Posting Pro

Re: Remove dublicates from std::vector<string>

 
0
  #8
Mar 10th, 2008
Thanks, cbattagler. I red the article. It was interesting and found out how to iterate on a push_back vector, though my output file does not still give unique records :\

The vector I have consist of this:

5,1
5,2
5,3
5,4
5,2
5,5

The code I have this far look like this:
  1. std::sort(ReadInData.begin(), ReadInData.end());
  2. vector<string>::iterator new_end = std::unique(ReadInData.begin(), ReadInData.end());
  3.  
  4.  
  5. for (std::vector<string>::iterator it = ReadInData.begin(); it != ReadInData.end(); ++it)
  6. {
  7. outFile << *it << '\n';
  8. }

The output I get on the outFile look like this, so there is still duplicates. (5,2 5,2).
I am not sure how to continue from here. Thanks.

5,1
5,2
5,2
5,3
5,4
5,5
Last edited by Jennifer84; Mar 10th, 2008 at 6:56 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Remove dublicates from std::vector<string>

 
0
  #9
Mar 10th, 2008
You need to use the new_end iterator that the unique() produced.
std::sort(ReadInData.begin(), ReadInData.end());
vector<string>::iterator new_end = std::unique(ReadInData.begin(), ReadInData.end()); 
for (std::vector<string>::iterator it = ReadInData.begin(); it != new_end; ++it)
{
	outFile << *it << '\n';
}
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 517
Reputation: Jennifer84 is an unknown quantity at this point 
Solved Threads: 1
Jennifer84 Jennifer84 is offline Offline
Posting Pro

Re: Remove dublicates from std::vector<string>

 
0
  #10
Mar 10th, 2008
Yes, now I understand... that was the thing. Thousands of thanks

Originally Posted by mitrmkar View Post
You need to use the new_end iterator that the unique() produced.
std::sort(ReadInData.begin(), ReadInData.end());
vector<string>::iterator new_end = std::unique(ReadInData.begin(), ReadInData.end()); 
for (std::vector<string>::iterator it = ReadInData.begin(); it != new_end; ++it)
{
	outFile << *it << '\n';
}
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC