| | |
Remove dublicates from std::vector<string>
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 517
Reputation:
Solved Threads: 1
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
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.
•
•
Join Date: Jan 2008
Posts: 34
Reputation:
Solved Threads: 4
Easiest way to do it will be to #include algorithm and sort the vector, and then use unique
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
c++ Syntax (Toggle Plain Text)
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
•
•
Join Date: Feb 2008
Posts: 517
Reputation:
Solved Threads: 1
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.
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.
C++ Syntax (Toggle Plain Text)
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.
•
•
Join Date: Feb 2008
Posts: 517
Reputation:
Solved Threads: 1
Yes okay... thank you. I did just wonder a bit there 
When have written this, I am not sure exactly what has happend here:
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.
Thank you...

When have written this, I am not sure exactly what has happend here:
C++ Syntax (Toggle Plain Text)
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.
C++ Syntax (Toggle Plain Text)
for (std::vector<string>::iterator new_end) { outFile << ReadInData[].c_str() << '\n'; }
Thank you...
Last edited by Jennifer84; Mar 10th, 2008 at 6:22 pm.
•
•
Join Date: Jan 2008
Posts: 34
Reputation:
Solved Threads: 4
This is my favorite article about using iterators, check it out http://www.oreillynet.com/pub/a/netw...us.html?page=2
Cameron
Cameron
•
•
Join Date: Feb 2008
Posts: 517
Reputation:
Solved Threads: 1
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:
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
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:
C++ Syntax (Toggle Plain Text)
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
Last edited by Jennifer84; Mar 10th, 2008 at 6:56 pm.
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
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';
}•
•
Join Date: Feb 2008
Posts: 517
Reputation:
Solved Threads: 1
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'; }
![]() |
Other Threads in the C++ Forum
- Previous Thread: C++ operator overloading.....problem with compiling
- Next Thread: Please help me.
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






