C++ 標準庫中 用於數組的begin和end函數

C++ 標準庫中 用於數組的begin和end函數

#include <iostream>
#include <iterator> //begin和end函數定義在iterator文件中
using namespace std;
int ia[] = {0,1,2,3,4,5,-10,6,7,8};
int *beg = begin(ia); //begin函數返回指向ia首元素的指針,end函數返回指向ia尾元素下一位置的指針。
int *last = end(ia);
int main()
{
/*
返回數組ia中的首個非0數
*/
	while (beg != last && *beg >=0)
		++beg;
	cout << *beg;
   return 0;
}

數組形參

C++ 中數組的兩個性質:

  1. 不允許拷貝數組
  2. 使用數組時會將其轉換成指針

數組引用形參

void print (int (&arr) [10]) 
// &arr兩端的括號必不可少
{
    for (auto elem : arr)
        cout<< elem << endl;
}

// 數組作爲函數形參的形式
void print (const int*);

void print (const int []);

void print (const int[10]); 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章