C 中for迴圈遍歷容器

2021-09-23 08:13:12 字數 437 閱讀 9161

基於範圍的for迴圈

#include

#include

#include

using namespace std;

vectormy_array = ;

方式一:原始方法

for (int x = 0; x < my_array.size(); x++)

方式二:用迭代器

for (auto it = my_array.begin(); it != my_array.end(); ++it)

方式三:c++11特性,加&可以修改vector中的元素

vectormy_array = ;

// 每個陣列元素乘於 2

for (int &x : my_array)

;// 每個陣列元素乘於 2

for (int x : my_array)

C 中的map容器的刪除 插入 遍歷

include include include include using namespace std struct display intmain else 使用迭代器完成遍歷 for iter scores.begin iter scores.end iter 把scores值小於90的鍵值對刪...

關於迴圈遍歷map容器,erase元素的問題

發現跳過了c,這是為什麼?原因 我們假設迭代器有6個位置0,1,2,3,4,5,對應a,b,c,d,e,f,當刪除b後 內容變成了0,1,2,3,4 對應a,c,d,e,f,迭代器位置為2 2的位置對應了d,跳過了c 正確用法 int main else std cout endl for std ...

總結js中的迴圈遍歷

定義乙個陣列和物件 const arr a b c d e f const obj 經常用來遍歷陣列元素 遍歷值為陣列元素索引for let i 0 len arr.length,i len i 用來遍歷陣列元素 第乙個引數為陣列元素,第二個引數為陣列元素索引,第三個引數為陣列本身 可選 沒有返回值...