C 通過容器vector遍歷陣列兩種方式

2021-10-04 14:58:25 字數 914 閱讀 1051

#include

#include

#include

using

namespace std;

template

<

typename t>

void

printele

(t value)

void

test()

//第一種遍歷方式

//使用for迴圈和迭代器遍歷容器

//begin()返回的迭代器指向容器中的首元素

//end()返回的迭代器指向容器中尾元素的下乙個位置

for(vector<

int>

::iterator iter = v.

begin()

; iter != v.

end(

); iter++

)//第二種遍歷方式

//需要包含標頭檔案

//第三個引數需要傳入函式名,即對容器中元素的操作方法

for_each

(v.begin()

, v.

end(

), printele<

int>);

}int

main()

for_each的底層實現:

// function template for_each

template

<

class

_init

,class

_fn>

_fn for_each

(_init _first, _init _last, _fn _func)

return _func;

}

python遍歷陣列

os.file split e python python25 lib os.pyc os.path.split os.file e python python25 lib os.pyc var myarr array new array one two three var mystr string...

Jquery遍歷陣列

each 方法 ul li input checked each function i,el 在乙個列表中,迴圈每一項用each是不錯的,索引,元素都給遍歷出來。each 方法 1.處理一維陣列,如下 each a b c function i,el 控制台輸出 0 a 1 b 2 c 2.處理二維...

遍歷陣列元素

遍歷陣列元素也就是把每個元素輸出出來.第一種語法 foreach arr as val ue value value value是自己定義的,陣列中每個元素的值給val ue,然 後輸出第 二種語法 for each value,然後輸出 第二種語法 foreach value,然後輸出 第二種語 ...