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

使用“for"循環遍歷 C++ 向量

Iterate through a C++ Vector using a #39;for#39; loop(使用“for循環遍歷 C++ 向量)
本文介紹了使用“for"循環遍歷 C++ 向量的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我是 C++ 語言的新手.我已經開始使用向量,并注意到在我看到的所有通過索引迭代向量的代碼中,for 循環的第一個參數總是基于向量的東西.在 Java 中,我可能會用 ArrayList 做這樣的事情:

I am new to the C++ language. I have been starting to use vectors, and have noticed that in all of the code I see to iterate though a vector via indices, the first parameter of the for loop is always something based on the vector. In Java I might do something like this with an ArrayList:

for(int i=0; i < vector.size(); i++){
   vector[i].doSomething();
}

有什么原因我在 C++ 中看不到這個嗎?這是不好的做法嗎?

Is there a reason I don't see this in C++? Is it bad practice?

推薦答案

有什么原因我在 C++ 中看不到這個嗎?這是不好的做法嗎?

沒有.這不是一個壞習慣,但以下方法使您的代碼具有一定的靈活性.

No. It is not a bad practice, but the following approach renders your code certain flexibility.

通常,在 C++11 之前,迭代容器元素的代碼使用迭代器,例如:

Usually, pre-C++11 the code for iterating over container elements uses iterators, something like:

std::vector<int>::iterator it = vector.begin();

這是因為它使代碼更加靈活.

This is because it makes the code more flexible.

所有標準庫容器都支持并提供迭代器.如果在以后的開發中需要切換到另一個容器,則不需要更改此代碼.

All standard library containers support and provide iterators. If at a later point of development you need to switch to another container, then this code does not need to be changed.

注意:編寫適用于所有可能的標準庫容器的代碼并不像看起來那么容易.

Note: Writing code which works with every possible standard library container is not as easy as it might seem to be.

這篇關于使用“for"循環遍歷 C++ 向量的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

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()之間的區別與“if())
C++, variable declaration in #39;if#39; expression(C++,if 表達式中的變量聲明)
主站蜘蛛池模板: 毛片一区二区三区 | 国产欧美精品一区二区三区 | 日韩成人中文字幕 | 精品国产一区二区三区久久影院 | 超碰520| 国产精品美女久久久av超清 | 日韩欧美成人精品 | 日韩欧美国产成人一区二区 | 日日夜夜精品免费视频 | 亚洲精品一区国产精品 | 亚洲欧美精品一区 | 亚洲网在线 | 韩日一区 | 国产成人小视频 | 在线成人福利 | 一区二区三区高清在线观看 | 国产91在线播放精品91 | 99精品免费久久久久久日本 | 久久久久无码国产精品一区 | 欧美一区二区三区国产 | 国产高清在线精品一区二区三区 | 亚洲成人三区 | 中文成人在线 | 色综合桃花网 | 我爱操 | 国产内谢| 中文字幕在线观看一区 | www.久久| 中文字幕不卡视频在线观看 | 蜜桃av人人夜夜澡人人爽 | 成人免费视频网站在线观看 | 日本三级在线 | 国产一区二区在线免费视频 | 一级黄色片在线看 | 国产精品久久av | 综合久久亚洲 | 中文字幕伊人 | 欧美激情综合五月色丁香小说 | com.色.www在线观看 | 精品一区二区三区在线观看 | 日本精品久久久久久久 |