为什么无法向关联容器的迭代器添加数字?

发布于 2024-10-04 17:06:26 字数 549 浏览 0 评论 0原文

我有一个 std::set 并且我想迭代集合中的元素对,所以我编写了 2 个 for 循环,如下所示:

for(std::set<T>::iterator i=mySet.begin();i!=mySet.end();++i)
{
    for(std::set<T>::iterator j=i+1;j!=mySet.end();++j)
    {
        // do something
    }
}

编译器告诉我不能向迭代器添加数字。但是我可以增加和减少它们。我发现我可以跳过第一次迭代的解决方法:

for(std::set<T>::iterator i=mySet.begin();i!=mySet.end();++i)
{
    std::set<T>::iterator j=i;
    for(++j;j!=mySet.end();++j)
    {
        // do something
    }
}

为什么我不能只添加一个数字,为​​什么我必须递增?

I have an std::set and I wanted to iterate trough the element pairs in the set so I wrote 2 for loops like this:

for(std::set<T>::iterator i=mySet.begin();i!=mySet.end();++i)
{
    for(std::set<T>::iterator j=i+1;j!=mySet.end();++j)
    {
        // do something
    }
}

The compiler told me that I can't add numbers to iterator. However I can increment and decrement them. A workaround I find that I can skip the first iteration:

for(std::set<T>::iterator i=mySet.begin();i!=mySet.end();++i)
{
    std::set<T>::iterator j=i;
    for(++j;j!=mySet.end();++j)
    {
        // do something
    }
}

Why I can't just add a number why I have to increment?

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

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

发布评论

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

评论(5

溺ぐ爱和你が 2024-10-11 17:06:26

您只能使用随机访问迭代器来执行此操作,因此原因与容器上没有索引操作符时速度缓慢的原因相同。另请参阅 为什么 std:: 没有运算符[]列表?

you can do this only with random access iterator, so the reasons are the same as for not having an indexoperator on containers when it would be slow. See also Why isn't there an operator[] for a std::list?

嘿咻 2024-10-11 17:06:26

和往常一样,Boost有一个解决方案:

for(std::set<T>::iterator i=mySet.begin();i!=mySet.end();++i)
{
    for(std::set<T>::iterator j=boost::next(i);j!=mySet.end();++j)
    {
        // do something
    }
}

当然你也可以考虑

for(std::set<T>::iterator i=mySet.begin();i!=mySet.end();/* NOTHING */)
{
    std::for_each(++i, mySet.end(), /* do something*/ );
}

As usual, Boost has a solution:

for(std::set<T>::iterator i=mySet.begin();i!=mySet.end();++i)
{
    for(std::set<T>::iterator j=boost::next(i);j!=mySet.end();++j)
    {
        // do something
    }
}

Of course you could also consider

for(std::set<T>::iterator i=mySet.begin();i!=mySet.end();/* NOTHING */)
{
    std::for_each(++i, mySet.end(), /* do something*/ );
}
山田美奈子 2024-10-11 17:06:26

因为set只支持双向迭代器,所以只允许递增和递减。

集合通常是树结构,因此这些是唯一有意义的操作。

Because set only supports bidirectional iterator, that only allows increments and decrements.

Set is usually a tree structure therefore these are the only operations that make sense.

终弃我 2024-10-11 17:06:26

请查看此处迭代器的定义。您正在寻找的方法仅为容器的迭代器定义,该迭代器允许随机访问而集合不支持。

Look here at the definition of iterators. The method which you are looking for is defined only for iterators of containers which allows random access which set doesn't support.

依 靠 2024-10-11 17:06:26

您的问题已经得到解答,但请注意,您可以像这样缩短代码:

for(std::set<T>::iterator i=mySet.begin();i!=mySet.end();++i)
{
    for(std::set<T>::iterator j=i;++j!=mySet.end();)
    {
        // do something
    }
}

Your question was already answered, but note that you can shorten your code like this:

for(std::set<T>::iterator i=mySet.begin();i!=mySet.end();++i)
{
    for(std::set<T>::iterator j=i;++j!=mySet.end();)
    {
        // do something
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文