views:

44

answers:

1

I have a vector of vectors:

vector< vector<int> > BigVec;

It contains an arbitrary number of vectors, each of an arbitrary size. I want to delete not duplicate elements of each vector, but any vectors that are the exact same as another. I don't need to preserve the order of the vectors so I can sort etc..

It should be a really simple problem to solve but I'm new to this, my (not-working) best effort:

for (int i = 0; i < BigVec.size(); i++)
  {
     for (int j = 1; j < BigVec.size() ; j++ )
        {
             if (BigVec[i][0] == BigVec [j][i]);
             {
                BigVec.erase(BigVec.begin() + j);
                i = 0;       // because i get the impression deleting a 
                j = 1;       // vector messes up a simple iteration through
             }
        }
  }

I think there might be a solution using Unique(), but I can't get that to work either.

+2  A: 

Why not use unique? I think if you're having problems getting it to work it's because using std::unique requires that the range be sorted. So, something like

std::vector<std::vector<int>> myVec;
std::sort(myVec.begin(), myVec.end());
myVec.erase(std::unique(myVec.begin(), myVec.end()), myVec.end());
Billy ONeal
Aha, I was getting the syntax wrong when I tried it. Much appreciated.
David
Yes, there is an `operator<` for `std::vector`. You need to use the `vector::erase` that has two parameters, though; the one you are using deletes only one element; you need to delete all of the elements after the iterator returned by `std::unique`.
James McNellis
@James McNellis: Thank you. Fixed.
Billy ONeal