×

C++ STL Tutorial

C++ STL Algorithm

C++ STL Arrays

C++ STL String

C++ STL List

C++ STL Stack

C++ STL Set

C++ STL Queue

C++ STL Vector

C++ STL Map

C++ STL Multimap

C++ STL MISC.

array::begin() and array::end() functions with Example in C++ STL

C++ STL | array::begin() and array::end() functions: Here, we are going to learn about the array::begin() and array::end() functions of Array in C++ STL.
Submitted by IncludeHelp, on March 01, 2019

C++ STL array::begin() and array::end() functions

array::begin() function is a library function of array and it is used to get the first element of the array, it returns an iterator pointing to the first element of the array.

array::end() function is a library function of array and it is used to get the last element of the array, it returns an iterator pointing to the last element of the array.

Syntax

array::begin();
array::end();

Parameter(s)

None

Return value

Function return iterators pointing to the first and last elements of an array.

Sample Input and Output

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

Example

C++ STL program to demonstrate example of array::begin() and array::end() functions:

#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;
}

Output

Elements of numbers array...
10 20 30 40 50
Elements of cities array...
New Delhi Mumbai Gwalior

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.