How to get the first element of an array in C++
In this post we will give you information about How to get the first element of an array in C++. Hear we will give you detail about How to get the first element of an array in C++And how to use it also give you demo for it if it is necessary.
we are going to learn about how to get the first element of an array in C++.
Consider, we have the following array.
array<int,4> nums {20, 30, 40, 50};
Now, we want to get the first element 20 from the above array.
To get the first element (20) from the array, we can use the subscript operator [ ] by passing an index 0.
In C++ arrays are zero-indexed, so the first element index of an array is 0.
Here is an example:
#include <iostream>#include <array>using namespace std;int main(){ array<int,4> nums {20, 30, 40, 50}; int firstElement = nums[0]; cout<<"First element is: "<<firstElement<<endl; return 0;}
Output:
20
Hope this code and post will helped you for implement How to get the first element of an array in C++. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs