Get first and last elements from Array and Vector in CPP
Given an array, find first and last elements of it.
Input: {4, 5, 7, 13, 25, 65, 98} Output: First element: 4 Last element: 98
In C++, we can use sizeof operator to find number of elements in an array.
// C++ Program to print first and last element in an array #include <iostream> using namespace std; int main() { int arr[] = { 4, 5, 7, 13, 25, 65, 98 }; int f, l, n; n = sizeof (arr) / sizeof (arr[0]); f = arr[0]; l = arr[n - 1]; cout << "First element: " << f << endl; cout << "Last element: " << l << endl; return 0; } |
First element: 4 Last element: 98
We should not sizeof when arrays are passed as parameters, there we must pass size and use that size to find first and last elements.
// C++ Program to print first and last element in an array #include <iostream> using namespace std; int printFirstLast( int arr[], int n) { int f = arr[0]; int l = arr[n - 1]; cout << "First element: " << f << endl; cout << "Last element: " << l << endl; } int main() { int arr[] = { 4, 5, 7, 13, 25, 65, 98 }; int n = sizeof (arr) / sizeof (arr[0]); printFirstLast(arr, n); return 0; } |
First element: 4 Last element: 98
In case of vectors in C++, there are functions like front and back to find first and last elements.
// C++ Program to find first and last elements in vector #include <iostream> #include <vector> using namespace std; int main() { vector< int > v; v.push_back(4); v.push_back(5); v.push_back(7); v.push_back(13); v.push_back(25); v.push_back(65); v.push_back(98); cout << "v.front() is now " << v.front() << '\n' ; cout << "v.back() is now " << v.back() << '\n' ; return 0; } |
v.front() is now 4 v.back() is now 98
We can use front and back even when vector is passed as a parameter.
Recommended Posts:
- How to find the sum of elements of a Vector using STL in C++?
- How to find common elements between two Vector using STL in C++?
- Count elements in a vector that match a target value or condition
- Advantages of vector over array in C++
- Convert an array to reduced form | Set 2 (Using vector of pairs)
- vector::empty() and vector::size() in C++ STL
- vector :: cbegin() and vector :: cend() in C++ STL
- vector::push_back() and vector::pop_back() in C++ STL
- vector::front() and vector::back() in C++ STL
- vector::crend() & vector::crbegin() with example
- Generate original array from an array that store the counts of greater elements on right
- vector::at() and vector::swap() in C++ STL
- vector::begin() and vector::end() in C++ STL
- Find elements of an Array which are Odd and Even using STL in C++
- How to find the sum of elements of an Array using STL in C++?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.