陣列的遍歷和元素輸出的四種方法

2021-06-22 17:10:43 字數 1038 閱讀 5341

環境需要vs2012以上

#include

using namespace std;

int main()

;//遍歷陣列並輸出陣列元素的4中方法

cout << "第一種輸出:" << endl;

for (auto &ai : a)       //利用for迴圈,遍歷外層陣列,這是ai必須是引用

for (auto i : ai)      //利用for迴圈遍歷內層陣列

cout << i << " ";     //輸出每乙個陣列元素

cout << endl;

cout << "第二種是輸出:" << endl;

for (int i = 0; i != 3; ++i)    //先遍歷外層陣列   

for (int j = 0; j != 4; ++j)   //再遍歷內層陣列

cout << a[i][j] << " ";    //通過下標輸出對應元素

cout << endl;

cout << "第三種輸出:" << endl;

for (auto p = a; p != a + 3; ++p)   //p指向含有4個整數的陣列,p=a就是p指向a的第乙個內層陣列

for (auto q = *p; q != *p + 4; ++q)  //q指向4個整數陣列的首元素,也就是說q指向乙個整數

cout << *q << " ";     //通過解引用輸出陣列元素的值

cout << endl;

cout << "第四種輸出" << endl;

for (auto p = begin(a); p != end(a); ++p)   //用p(begin(a))指向a的內層陣列的第乙個陣列,用end(a)指向a的尾

for (auto q = begin(*p); q != end(*p); ++q)  //q指向內層陣列的第乙個元素。

cout << *q << " ";       //用解引用輸出元素值

cout << endl;

return 0;}

遍歷HashMap的四種方法

public static void main string args 第二種 system.out.println 通過map.entryset使用iterator遍歷key和value iterator it map.entryset iterator while it.hasnext 第三種 ...

遍歷Map的四種方法

public static void main string args 第二種 system.out.println 通過map.entryset使用iterator遍歷key和value iterator it map.entryset iterator while it.hasnext 第三種 ...

遍歷Map的四種方法

第一種 普遍使用,二次取值 system.out.println 通過map.keyset遍歷key和value for string key map.keyset 第二種 system.out.println 通過map.entryset使用iterator遍歷key和value iterator...