I have a STL vector of STL vector which contains integer values. Some inside vectors are duplicating but their element order is not the same. Now, I want to get a vector of vector without having any duplicate inner vectors. I’ve been seen the following method:
std::vector<std::vector<int>> myVec;
std::sort(myVec.begin(), myVec.end());
myVec.erase(std::unique(myVec.begin(), myVec.end()), myVec.end());
the problem is that i want to eleminate duplicates keeping the order of elements of each order(original order or without sort it), what is the best way to do this? is there another way more efficient?
Example:
1 6 4 5
3 1 5 2----> result of elimination: 1 6 4 5
2 1 3 5 3 1 5 2
Thanks in advance
vacing
The question is not exactly clear. So I am going to give two answers.
(1) If you wish to remove duplicates but retaining 1 copy while maintaining
myVec‘s order, you need to use a set.(2) If you wish to remove all elements that appear more than once in
myVecyou need a map of counters.Both solutions respects the order of elements in
myVecand retains the original order in the inner vectors’ elements.