Working with Array and Vectors using STL in C++

Array and vectors are very important data structures in competitive programming for solving problems. And the STL (Standard Template Library) in c++ programming provide some functions to perform operations of arrays and vectors.
Let’s see some of these functions in action,
Finding sum, Min and Max of the array/vector − In STL there are function that helps you to find the sum, max, and min of the array/vector. Functions with there function,
accumulate(startIndex, endIndex, initialSum)
*max_element(startIndex, endIndex)
*min_element(startIndex, endIndex)
Program to perform operations on array −
 Live Demo
#include <bits/stdc++.h>
using namespace std;
int main(){
   int array[] = {65, 7,12, 90, 31, 113 };
   int l = sizeof(array) / sizeof(array[0]);
   cout<<"The elments of the array are : ";
   for(int i = 0; i<l; i++)
   cout<<array[i]<<"\t";
   cout<<endl;
   cout<<"The sum of all elements of the array: "<<accumulate(array, array + l, 0)<<endl;
   cout<<"The element with maximum value in array: "<<*max_element(array, array + l)<<endl;
   cout<<"The element with minimum value in array: "<<*min_element(array, array + l)<<endl;
   return 0;
}
The elments of the array are : 65 7 12 90 31 113
The sum of all elements of the array: 318
The element with maximum value in array: 113
The element with minimum value in array: 7
Program to perform operations on vector −
 Live Demo
#include <bits/stdc++.h>
using namespace std;
int main(){
   vector<int> vec= {65, 7,12, 90, 31, 113 };
   cout<<"The sum of all elments of the vector: "<<accumulate(vec.begin(), vec.end() + 1, 0)<<endl;
   cout<<"The element with maximum value in vector: "<<*max_element(vec.begin(), vec.end())<<endl;
   cout<<"The element with minimum value in vector: "<<*min_element(vec.begin(), vec.end())<<endl;
   return 0;
}
The sum of all elments of the vector: 318
The element with maximum value in vector: 113
The element with minimum value in vector: 7
Sorting elements of the array/vector −
In STL, there is a function sort() which can be used to sort elements of the array/vector. The function uses quick sort technique for sorting array/vector.
sort(startIndex, endIndex)
Program to sort elements of the array −
 Live Demo
#include <bits/stdc++.h>
using namespace std;
int main(){
   int array[] = {65, 7,12, 90, 31, 113 };
   int l = sizeof(array) / sizeof(array[0]);
   cout<<"The elments of the array are : ";
   for(int i = 0; i<l; i++)
      cout<<array[i]<<"\t";
   cout<<endl;
   cout<<"\nSorting elements of the array...\n\n";
   sort(array, array+l);
   cout<<"The sorted array is : ";
   for(int i = 0; i<l; i++)
      cout<<array[i]<<"\t";
   cout<<endl;
   return 0;
}
The elments of the array are : 65 7 12 90 31 113
Sorting elements of the array...
The sorted array is : 7 12 31 65 90 113
Program to sort elements of vector −
 Live Demo
#include <bits/stdc++.h>
using namespace std;
int main(){
   vector<int> vec = {65, 7,12, 90, 31, 113 };
   cout<<"The elments of the vector are : ";
   for(int i = 0; i<vec.size(); i++)
      cout<<vec[i]<<"\t";
   cout<<endl;
   cout<<"\nSorting elements of the vector...\n\n";
   sort(vec.begin(), vec.end());
   cout<<"The sorted vector is : ";
   for(int i = 0; i<vec.size(); i++)
      cout<<vec[i]<<"\t";
   cout<<endl;
   return 0;
}
The elments of the vector are : 65 7 12 90 31 113
Sorting elements of the vector...
The sorted vector is : 7 12 31 65 90 113