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

Recommended Answers

All 9 Replies

Easiest way to do it will be to #include algorithm and sort the vector, and then use unique

std::sort(Values.begin(), Values.end());
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

>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.

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.

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

>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.

Oops, that was my fault, just cut and pasted some code I was working on change Lidar to string in your case..

Cameron

Yes okay... thank you. I did just wonder a bit there :)

When have written this, I am not sure exactly what has happend here:

std::sort(ReadInData.begin(), ReadInData.end());
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.

for (std::vector<string>::iterator new_end)
{
     outFile << ReadInData[].c_str() << '\n';
}

Thank you...

Oops, that was my fault, just cut and pasted some code I was working on change Lidar to string in your case..

Cameron

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:

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 != ReadInData.end(); ++it)
{
	outFile << *it << '\n';
}

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

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';
}

Yes, now I understand... that was the thing. Thousands of thanks :)

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';
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.