array::begin()函数是array的库函数,用于获取数组的第一个元素,它返回一个迭代器,该迭代器指向数组的第一个元素。
array::end()函数是array的库函数,用于获取数组的最后一个元素,它返回一个指向数组最后一个元素的迭代器。
语法:
array::begin(); array::end();
参数:无
返回值:函数返回迭代器指向数组的第一个和最后一个元素。
示例
Input or array declaration:
array<int,5> arr {10, 20, 30, 40, 50};
Function call:
auto it=arr.begin();
cout<<*it;
it=arr.end();
cout<<*it;
Output:
10 50#include <array>
#include <iostream>
using namespace std;
int main(){
array<int,5> numbers {10, 20, 30, 40, 50};
array<string,5> cities {"New Delhi", "Mumbai", "Gwalior"};
cout<<"Elements of numbers array..."<<endl;
for(auto it=numbers.begin(); it!=numbers.end(); it++)
cout<<*it<<" ";
cout<<endl;
cout<<"Elements of cities array..."<<endl;
for(auto it=cities.begin(); it!=cities.end(); it++)
cout<<*it<<" ";
cout<<endl;
return 0;
}输出结果
Elements of numbers array... 10 20 30 40 50 Elements of cities array... New Delhi Mumbai Gwalior