久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

C++ 中類似 Python 的循環(huán)枚舉

Python-like loop enumeration in C++(C++ 中類似 Python 的循環(huán)枚舉)
本文介紹了C++ 中類似 Python 的循環(huán)枚舉的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

可能的重復:
在 C++11 range-based 中查找元素的位置for 循環(huán)?

我有一個 vector 并且我想迭代它,同時可以訪問每個單獨元素的索引(我需??要將元素及其索引傳遞給功能).我考慮了以下兩種解決方案:

I have a vector and I would like to iterate it and, at the same time, have access to the indexes for each individual element (I need to pass both the element and its index to a function). I have considered the following two solutions:

std::vector<int> v = { 10, 20, 30 };

// Solution 1
for (std::vector<int>::size_type idx = 0; idx < v.size(); ++idx)
    foo(v[idx], idx);

// Solution 2
for (auto it = v.begin(); it != v.end(); ++it)
    foo(*it, it - v.begin());

我想知道是否有更緊湊的解決方案.類似于 Python 的 enumerate.這是我使用 C++11 范圍循環(huán)得到的最接近的結果,但必須在私有范圍內定義循環(huán)外的索引似乎比 1 或 2 更糟糕:

I was wondering whether there might be a more compact solution. Something similar to Python's enumerate. This is the closest that I got using a C++11 range-loop, but having to define the index outside of the loop in a private scope definitely seems to be like a worse solution than either 1 or 2:

{
    int idx = 0;
    for (auto& elem : v)
        foo(elem, idx++);
}

有沒有辦法(也許使用 Boost)來簡化最新的例子,使索引自包含到循環(huán)中?

Is there any way (perhaps using Boost) to simplify the latest example in such a way that the index gets self-contained into the loop?

推薦答案

正如@Kos 所說,這是一件如此簡單的事情,我真的不認為有必要進一步簡化它,個人只會堅持傳統(tǒng)的使用索引循環(huán),除了我會放棄 std::vector::size_type 并簡單地使用 std::size_t:

As @Kos says, this is such a simple thing that I don't really see the need to simplify it further and would personally just stick to the traditional for loop with indices, except that I'd ditch std::vector<T>::size_type and simply use std::size_t:

for(std::size_t i = 0; i < v.size(); ++i)
    foo(v[i], i);

我不太熱衷于解決方案 2.它需要(有點隱藏)隨機訪問迭代器,這不允許您輕松交換容器,這是迭代器的強項之一.如果您想使用迭代器并使其通用(并且當迭代器不是隨機訪問時可能會導致性能下降),我建議使用std::distance:

I'm not too keen on solution 2. It requires (kinda hidden) random access iterators which wouldn't allow you to easily swap the container, which is one of the strong points of iterators. If you want to use iterators and make it generic (and possibly incur a performance hit when the iterators are not random access), I'd recommend using std::distance:

for(auto it(v.begin()); it != v.end(); ++it)
    foo(*it, std::distance(it, v.begin());

這篇關于C++ 中類似 Python 的循環(huán)枚舉的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯(lián)網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

What do compilers do with compile-time branching?(編譯器如何處理編譯時分支?)
Can I use if (pointer) instead of if (pointer != NULL)?(我可以使用 if (pointer) 而不是 if (pointer != NULL) 嗎?)
Checking for NULL pointer in C/C++(在 C/C++ 中檢查空指針)
Math-like chaining of the comparison operator - as in, quot;if ( (5lt;jlt;=1) )quot;(比較運算符的數學式鏈接-如“if((5<j<=1)))
Difference between quot;if constexpr()quot; Vs quot;if()quot;(“if constexpr()之間的區(qū)別與“if())
C++, variable declaration in #39;if#39; expression(C++,if 表達式中的變量聲明)
主站蜘蛛池模板: 狠狠干天天干 | 国产精品美女久久久久久久网站 | 日韩在线免费播放 | 国产精品中文字幕在线 | 国产极品粉嫩美女呻吟在线看人 | 欧美视频在线播放 | www日韩 | 中文字幕一区二区三区四区 | 黄色片亚洲 | 国产精品一区在线观看你懂的 | 国产高清精品在线 | 日本黄色高清视频 | 在线观看日本高清二区 | 欧美日一区 | 亚洲激情一区二区三区 | 91免费在线 | 中文字幕在线一区二区三区 | 久久久久久91香蕉国产 | 国产不卡一区 | 在线a视频| 天天夜夜操 | 91精品国产综合久久久久久丝袜 | 一级欧美一级日韩片免费观看 | 亚洲一区二区三区在线播放 | 亚洲精品99 | 日朝毛片| 亚洲二区在线 | 午夜一区二区三区 | 欧美一级二级视频 | 激情国产视频 | 免费的av网站 | 亚洲日日| 欧美成人一区二区三区 | 国产精品亚洲一区二区三区在线 | 天天澡天天狠天天天做 | 久在线| 男人的天堂久久 | 国产精品伦理一区二区三区 | 在线观看特色大片免费网站 | 免费激情 | h视频免费在线观看 |