删除向量向量内的重复向量

发布于 2024-12-07 09:16:24 字数 1515 浏览 1 评论 0原文

我有一个向量向量(循环),其中包含整数值。一些内部向量是重复的,但它们的元素顺序不同。现在,我想获得一个向量的向量,而没有任何重复的内部向量。 这是我的 vec 的一个例子;

循环 = ((9 18 26 11 9), (9 11 26 18 9),(9 18 25 16 9),(11 45 26 11),( 11 26 45 11),( 16 49 25 16),( 16 25 49 16),(18 9 11 26 18),( 18 9 16 25 18),( 25 16 49 25),( 26 11 45 26))

识别任何内部向量是否是另一个内部向量的重复项;我开发了一个函数IsDuplicate。这告诉我,(9 18 26 11 9) 和 (9 11 26 18 9) 是重复的,那么我可以删除第二个或所有其他重复项。

为了删除向量向量中的重复向量,我实现了以下代码。

Vector<vector<int> > loops;
Vector<vector<int> > ::iterator no1, no2;
Int setno1, setno2;

for (no1=loops.begin(), setno1=0; no1!=loops.end(); no1++, setno1++){
       set1 = *no1;
       for (no2=loops.begin()+setno1, setno2=setno1; no2!=loops.end(); setno2++){
            set2 = *no2;
            if (set2.IsDuplicate(set1))  loops.erase(loops.begin()+setno2);
            else no2++;
       }

  }

这花了很长时间,我以为我的程序崩溃了。所以,请帮助我纠正这个问题。

另外,我尝试过这个。这有效,但我得到了错误的答案。请提供任何帮助。

01   int first=0; bool duplicates=false;  
02   do {     
03        set1 = loops[first];     
04        for (no2=loops.begin()+1, setno2=1;  no2!=loops.end();  setno2++){     
05             set2 = *no2;      
06             if (set2.IsPartOf(set1)){      
07                 loops.erase(loops.begin()+setno2);     
08                 duplicates = true;      
09             }      
10             else no2++;     
11        }      
12        first++;      
13       } while(!duplicates); 

I have a vector of vector (loops) 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.
here is an example for my vec of vec;

loops = ((9 18 26 11 9), (9 11 26 18 9),(9 18 25 16 9),(11 45 26 11),( 11 26 45 11),( 16 49 25 16),( 16 25 49 16),(18 9 11 26 18),( 18 9 16 25 18),( 25 16 49 25),( 26 11 45 26))

To identify whether any inner vector is a duplicate of another inner vector; I have developed a function IsDuplicate. This tells me, (9 18 26 11 9) and (9 11 26 18 9) are duplicates then I can delete the second or all other duplicates.

To remove duplicate vectors inside my vector of vector, I have implemented following codes.

Vector<vector<int> > loops;
Vector<vector<int> > ::iterator no1, no2;
Int setno1, setno2;

for (no1=loops.begin(), setno1=0; no1!=loops.end(); no1++, setno1++){
       set1 = *no1;
       for (no2=loops.begin()+setno1, setno2=setno1; no2!=loops.end(); setno2++){
            set2 = *no2;
            if (set2.IsDuplicate(set1))  loops.erase(loops.begin()+setno2);
            else no2++;
       }

  }

it took very very long time and i thought my program is crasihing. so, Please help me to rectify this issue.

also, i tried with this. this works but i got a wrong answer. any help please.

01   int first=0; bool duplicates=false;  
02   do {     
03        set1 = loops[first];     
04        for (no2=loops.begin()+1, setno2=1;  no2!=loops.end();  setno2++){     
05             set2 = *no2;      
06             if (set2.IsPartOf(set1)){      
07                 loops.erase(loops.begin()+setno2);     
08                 duplicates = true;      
09             }      
10             else no2++;     
11        }      
12        first++;      
13       } while(!duplicates); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Smile简单爱 2024-12-14 09:16:24

惯用的方法是将 Erase/Remove idiom 与自定义谓词一起使用。
要检查重复向量而不修改向量内容,请编写一个按值获取参数的谓词,对向量进行排序并使用 std::equal

bool equal_vector(std::vector<int> a, std::vector<int> b) {
  std::sort(a.begin(), a.end());
  std::sort(b.begin(), b.end());

  return std::equal(a.begin(), a.end(), b.begin());
}

// use it like this
v.erase( remove_if(v.begin(), v.end(), equal_vector), v.end() );

至于当前代码失败的原因:从 vector 中删除元素会使当前存在的该向量的所有其他迭代器无效,因此 vector::erase 返回一个有效的迭代器已删除元素之后的位置。

stdlib 还提供了 set 和 multiset 容器,它们看起来更适合您的目的。

The idiomatic way is to use the Erase/Remove idiom with a custom predicate.
To check for duplicate vectors and without modifying the contents of your vectors, write a predicate that takes its arguments by value, sort the vectors and use std::equal.

bool equal_vector(std::vector<int> a, std::vector<int> b) {
  std::sort(a.begin(), a.end());
  std::sort(b.begin(), b.end());

  return std::equal(a.begin(), a.end(), b.begin());
}

// use it like this
v.erase( remove_if(v.begin(), v.end(), equal_vector), v.end() );

As to why your current code fails: Erasing an element from a vector invalidates all other iterators to that vector that are currently in existence thus vector::erase returns a valid iterator to the position after the element that has been removed.

The stdlib also provides the set and multiset container which look like a much better fit for your purpose.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文