如何从 stl 集中获取唯一的值对

发布于 2024-09-17 05:02:10 字数 536 浏览 3 评论 0原文

我有一组 stl 整数,我想迭代所有唯一的整数值对,其中通过唯一性,我认为 val1,val2 和 val2,val1 是相同的,我应该只看到该组合一次。

我在 python 中编写了这个,其中使用列表(簇)的索引:

for i in range(len(clusters) - 1):
    for j in range(i+1,len(clusters)):
       #Do something with clusters[i],clusters[j])

但是如果没有索引,我不确定如何使用 stl 集和迭代器实现相同的效果。我尝试过:

for (set<int>::iterator itr = myset.begin(); itr != myset.end()-1; ++itr) {
    cout << *itr;
}

但这失败了,因为迭代器没有 - 运算符。

我怎样才能实现这一点,或者我必须使用不同的容器?

I have a stl set of integers and I would like to iterate through all unique pairs of integer values, where by uniqueness I consider val1,val2 and val2,val1 to be the same and I should only see that combination once.

I have written this in python where I use the index of a list (clusters):

for i in range(len(clusters) - 1):
    for j in range(i+1,len(clusters)):
       #Do something with clusters[i],clusters[j])

but without an index I am not sure how I can achieve the same thing with a stl set and iterators. I tried out:

for (set<int>::iterator itr = myset.begin(); itr != myset.end()-1; ++itr) {
    cout << *itr;
}

but this fails as an iterator doesn't have a - operator.

How can I achieve this, or must I use a different container?

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

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

发布评论

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

评论(3

分分钟 2024-09-24 05:02:10

遵循以下几行怎么样:

for(set<int>::const_iterator iter1 = myset.begin(); iter1 != myset.end(); ++iter1) {
    for(set<int>::const_iterator iter2 = iter1; ++iter2 != myset.end();) {
    {
        std::cout << *iter1 << " " << *iter2 << "\n"; 
    }
}

这会产生所有 N*(N-1)/2 唯一对,其中 N 是集合中整数的数量。

顺便说一句:每当您迭代容器而不修改任何内容时,请使用 const_iterator ,它是很好的风格并且可能具有更好的性能。

编辑:修改代码以反映 Steve Jessop 提出的建议。

How about something along the following lines:

for(set<int>::const_iterator iter1 = myset.begin(); iter1 != myset.end(); ++iter1) {
    for(set<int>::const_iterator iter2 = iter1; ++iter2 != myset.end();) {
    {
        std::cout << *iter1 << " " << *iter2 << "\n"; 
    }
}

This yields all N*(N-1)/2 unique pairs, where N is the number of integers in your set.

As an aside: use a const_iterator whenever you iterate over a container without modifying anything, it's good style and might have better performance.

EDIT: Modified the code to reflect the suggestion made by Steve Jessop.

不念旧人 2024-09-24 05:02:10

您不需要执行 end() - 1,因为 end() 是一个指向容器中最后一个元素之后的迭代器。

更正后的代码为:

for (set<int>::iterator itr = myset.begin(); itr != myset.end(); ++itr) {
    for (set<int>::iterator itr2 = itr + 1; itr2 != myset.end(); ++itr2) {
        // Do whatever you want with itr and itr2
    }
}

You don't need to do end() - 1 since end() is an iterator that points after the last element in the container.

The corrected code is:

for (set<int>::iterator itr = myset.begin(); itr != myset.end(); ++itr) {
    for (set<int>::iterator itr2 = itr + 1; itr2 != myset.end(); ++itr2) {
        // Do whatever you want with itr and itr2
    }
}
旧情勿念 2024-09-24 05:02:10

将数据放入 boost::bimap 中,然后双向迭代,将结果复制到标准 STL 映射中,以强制唯一性。

Put your data in a boost::bimap, then iterate it both ways, copying the results into a standard STL map which will enforce uniqueness.

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