如何删除多维向量中的重复向量? [英] How to delete duplicate vectors within a multidimensional vector?

查看:312
本文介绍了如何删除多维向量中的重复向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个向量向量:

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

这应该是一个很简单的问题,但我是新的到这个,my(not-working)best effort:

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

我认为可能有一个解决方案使用Unique(),但我不能让它工作。

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

推荐答案

为什么不使用独特?我想如果你有问题让它工作,因为使用 std :: unique 要求范围排序。所以,像

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());

这篇关于如何删除多维向量中的重复向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆