Skip to content
Related Articles

Related Articles

Improve Article

Working with Array and Vectors using STL in C++

  • Difficulty Level : Easy
  • Last Updated : 19 Mar, 2019

Using STL library it is very easy to perform certain basic operations on array like Sorting, searching, sum of elements, finding minimum and maximum element of the array.

Sorting

Sorting can be done with the help of sort() function.

sort(starting_index, last_index) – To sort the given array/vector. The sort() function works on quick sort algorithm. C++ STL provides a similar function sort that sorts a vector or array (items with random access). The complexity of it is O(nlogn).

Example:



Input: {1, 7, 2, 4, 8, 3}
Output: {1, 2, 3, 4, 7, 8}

Array




// C++ program to sort Array
// using sort() in STL
  
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    int a[] = { 1, 7, 2, 4, 8, 3 };
    int l = sizeof(a) / sizeof(a[0]);
    sort(a, a + l);
    for (int i; i < l; i++)
        cout << a[i] << " ";
    return 0;
}

Vector




// C++ program to sort Vector
// using sort() in STL
  
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    vector<int> a = { 1, 7, 2, 4, 8, 3 };
    sort(a.begin(), a.end());
    for (int i; i < a.size(); i++)
        cout << a[i] << " ";
    return 0;
}
Output:
1 2 3 4 7 8

Reverse

Reversing can be done with the help of reverse() function.

reverse(start_index, last_index): To reverse the given array/vector.

Example:

Input: {1, 7, 2, 4, 8, 3}
Output: {3, 8, 4, 2, 7, 1}

Array




// C++ program to reverse Array
// using reverse() in STL
  
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    int a[] = { 1, 7, 2, 4, 8, 3 };
    int l = sizeof(a) / sizeof(a[0]);
    reverse(a, a + l);
    for (int i = 0; i < l; i++)
        cout << a[i] << " ";
    return 0;
}

Vector




// C++ program to reverse Vector
// using reverse() in STL
  
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    vector<int> a = { 1, 7, 2, 4, 8, 3 };
    reverse(a.begin(), a.end());
    for (int i; i < a.size(); i++)
        cout << a[i] << " ";
    return 0;
}
Output:
3 8 4 2 7 1

Finding Sum, Maximum and Minimum element

The functions in STL to do the mentioned works are:

  • accumulate(first_index, last_index, initial value of sum): This function returns the sum of all elements of a array/vector.
  • *max_element (first_index, last_index): To find the maximum element of a array/vector.
  • *min_element (first_index, last_index): To find the minimum element of a array/vector.

Array




// C++ program to find sum, max and min
// element of Array using STL
  
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    int a[] = { 1, 7, 2, 4, 8, 3 };
    int l = sizeof(a) / sizeof(a[0]);
    cout << "\nsum of array: "
         << accumulate(a, a + l, 0);
    cout << "\nMaximum element in array: "
         << *max_element(a, a + l);
    cout << "\nMinimum element in array: "
         << *min_element(a, a + l);
    return 0;
}

Vector




// C++ program to find sum, max and min
// element of Vector using STL
  
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    vector<int> a = { 1, 7, 2, 4, 8, 3 };
    cout << "\nsum of vector: "
         << accumulate(a.begin(), a.end(), 0);
    cout << "\nMaximum element in vector: "
         << *max_element(a.begin(), a.end());
    cout << "\nMinimum element in vector: "
         << *min_element(a.begin(), a.end());
    return 0;
}
Output:
sum of array: 25
Maximum element in array: 8
Minimum element in array: 1
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :