簡體   English   中英

Go 到基於范圍的 for 循環中的下一個迭代器

[英]Go to next iterator in range-based for loop

對於我的項目,我需要使從循環到 go 的迭代器到容器中的下一個項目,執行一些操作,然后再次返回到同一個迭代器並繼續,但是,由於某種原因既不advance也不next然后使用上一個prev工作。 那么我怎樣才能獲得下一個迭代器並返回到上一個迭代器呢?

我收到以下錯誤消息:

no matching function for call to 'next(int&)'
no type named 'difference_type' in 'struct std::iterator_traits<int>'

謝謝!

template<class T>
void insert_differences(T& container)
{

    for(auto it : container){
        // do some operations here
        //advance(it,1);
        it = next(it); 
        // do some operations here 
        //advance(it, -1);
        it = prev(it);
        }

}

基於范圍的 for 循環迭代元素。 它的名稱在it令人困惑; 它不是迭代器而是元素,這就是為什么std::nextstd::prev不能使用它的原因。

在一個范圍內執行 for 循環。

用作對一系列值(例如容器中的所有元素)進行操作的傳統 for 循環的更易讀等價物。

您必須自己使用迭代器編寫循環,例如

for(auto it = std::begin(container); it != std::end(container); it++){
    // do some operations here
    //advance(it,1);
    it = next(it); 
    // do some operations here 
    //advance(it, -1);
    it = prev(it);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM