如何让迭代器加2?

发布于 2024-07-25 21:23:06 字数 102 浏览 4 评论 0原文

谁能告诉我如何将迭代器增加 2?

iter++ 可用 - 我必须执行 iter+2 吗? 我怎样才能实现这个目标?

Can anybody tell me how to increment the iterator by 2?

iter++ is available - do I have to do iter+2? How can I achieve this?

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

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

发布评论

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

评论(8

幽蝶幻影 2024-08-01 21:23:06

std::advance( iter, 2 );

此方法适用于不是随机访问迭代器的迭代器,但仍然可以通过实现进行专门化,使其在与随机访问迭代器一起使用时效率不低于 iter += 2

std::advance( iter, 2 );

This method will work for iterators that are not random-access iterators but it can still be specialized by the implementation to be no less efficient than iter += 2 when used with random-access iterators.

∞梦里开花 2024-08-01 21:23:06

http://www.cplusplus.com/reference/std/iterator/advance/

std::advance(it,n);

其中 n 在你的情况下是 2 。

该函数的优点在于,如果“it”是随机访问迭代器,则

it += n

使用快速操作(即向量<,,>::迭代器)。 否则它会渲染到

for(int i = 0; i < n; i++)
    ++it;

(即 list<..>::iterator)

http://www.cplusplus.com/reference/std/iterator/advance/

std::advance(it,n);

where n is 2 in your case.

The beauty of this function is, that If "it" is an random access iterator, the fast

it += n

operation is used (i.e. vector<,,>::iterator). Otherwise its rendered to

for(int i = 0; i < n; i++)
    ++it;

(i.e. list<..>::iterator)

柠栀 2024-08-01 21:23:06

如果您没有可修改的迭代器左值,或者需要获取给定迭代器的副本(保持原始迭代器不变),则 C++11 附带新的辅助函数 - std::next / std::prev

std::next(iter, 2);          // returns a copy of iter incremented by 2
std::next(std::begin(v), 2); // returns a copy of begin(v) incremented by 2
std::prev(iter, 2);          // returns a copy of iter decremented by 2

If you don't have a modifiable lvalue of an iterator, or it is desired to get a copy of a given iterator (leaving the original one unchanged), then C++11 comes with new helper functions - std::next / std::prev:

std::next(iter, 2);          // returns a copy of iter incremented by 2
std::next(std::begin(v), 2); // returns a copy of begin(v) incremented by 2
std::prev(iter, 2);          // returns a copy of iter decremented by 2
霓裳挽歌倾城醉 2024-08-01 21:23:06

您可以使用“加法赋值”运算符

iter += 2;

You could use the 'assignment by addition' operator

iter += 2;
烟花肆意 2024-08-01 21:23:06

我们可以使用 std::advance 以及 std::next,但是两者之间是有区别的。

advance 修改其参数并且不返回任何内容。 因此它可以用作:

vector<int> v;
v.push_back(1);
v.push_back(2);
auto itr = v.begin();
advance(itr, 1);          //modifies the itr
cout << *itr<<endl        //prints 2

next 返回迭代器的修改副本:

vector<int> v;
v.push_back(1);
v.push_back(2);
cout << *next(v.begin(), 1) << endl;    //prints 2

We can use both std::advance as well as std::next, but there's a difference between the two.

advance modifies its argument and returns nothing. So it can be used as:

vector<int> v;
v.push_back(1);
v.push_back(2);
auto itr = v.begin();
advance(itr, 1);          //modifies the itr
cout << *itr<<endl        //prints 2

next returns a modified copy of the iterator:

vector<int> v;
v.push_back(1);
v.push_back(2);
cout << *next(v.begin(), 1) << endl;    //prints 2
2024-08-01 21:23:06

如果您不知道容器中是否有足够的下一个元素,则需要在每个增量之间检查容器的末尾。 ++ 和 std::advance 都不会为你做这件事。

if( ++iter == collection.end())
  ... // stop

if( ++iter == collection.end())
  ... // stop

您甚至可以推出自己的绑定安全高级功能。

如果您确定不会超过末尾,那么 std::advance( iter, 2 ) 是最好的解决方案。

If you don't know wether you have enough next elements in your container or not, you need to check against the end of your container between each increment. Neither ++ nor std::advance will do it for you.

if( ++iter == collection.end())
  ... // stop

if( ++iter == collection.end())
  ... // stop

You may even roll your own bound-secure advance function.

If you are sure that you will not go past the end, then std::advance( iter, 2 ) is the best solution.

冷︶言冷语的世界 2024-08-01 21:23:06

假设列表大小可能不是步骤的偶数倍,您必须防止溢出:

static constexpr auto step = 2;

// Guard against invalid initial iterator.
if (!list.empty())
{
    for (auto it = list.begin(); /*nothing here*/; std::advance(it, step))
    {
        // do stuff...

        // Guard against advance past end of iterator.
        if (std::distance(it, list.end()) > step)
            break;
    }
}

根据集合实现,距离计算可能非常慢。 下面是最佳的并且更具可读性。 闭包可以更改为实用程序模板,其中列表结束值由 const 引用传递:

const auto advance = [&](list_type::iterator& it, size_t step)
{
    for (size_t i = 0; it != list.end() && i < step; std::next(it), ++i);
};

static constexpr auto step = 2;

for (auto it = list.begin(); it != list.end(); advance(it, step))
{
    // do stuff...
}

如果没有循环:

static constexpr auto step = 2;
auto it = list.begin();

if (step <= list.size())
{
    std::advance(it, step);
}

Assuming list size may not be an even multiple of step you must guard against overflow:

static constexpr auto step = 2;

// Guard against invalid initial iterator.
if (!list.empty())
{
    for (auto it = list.begin(); /*nothing here*/; std::advance(it, step))
    {
        // do stuff...

        // Guard against advance past end of iterator.
        if (std::distance(it, list.end()) > step)
            break;
    }
}

Depending on the collection implementation, the distance computation may be very slow. Below is optimal and more readable. The closure could be changed to a utility template with the list end value passed by const reference:

const auto advance = [&](list_type::iterator& it, size_t step)
{
    for (size_t i = 0; it != list.end() && i < step; std::next(it), ++i);
};

static constexpr auto step = 2;

for (auto it = list.begin(); it != list.end(); advance(it, step))
{
    // do stuff...
}

If there is no looping:

static constexpr auto step = 2;
auto it = list.begin();

if (step <= list.size())
{
    std::advance(it, step);
}
べ映画 2024-08-01 21:23:06

非常简单的答案:

++++iter

长答案:

你真的应该习惯于编写 ++iter 而不是 iter++。 后者必须返回旧值(的副本),该旧值与新值不同; 这需要时间和空间。

请注意,前缀增量 (++iter) 接受左值并返回左值,而后缀增量 (iter++) 接受左值并返回右值。

The very simple answer:

++++iter

The long answer:

You really should get used to writing ++iter instead of iter++. The latter must return (a copy of) the old value, which is different from the new value; this takes time and space.

Note that prefix increment (++iter) takes an lvalue and returns an lvalue, whereas postfix increment (iter++) takes an lvalue and returns an rvalue.

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