Open In App

Working with Array and Vectors using STL in C++

Improve
Improve
Like Article
Like
Save
Share
Report

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 time complexity of this function 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

Count

Count STL function is used to count the number of occurrences of a particular element in an array and a vector. This can be done with help of count() function, 
count (startindex , last index , element). 
Example:

C++




// C++ program to count no. of occurrence of element in an Array and Vector
// using count()
 
#include <bits/stdc++.h>
using namespace std;
 
int main()
{  
    //implementation of count in array
    // sample array
    int arr[] = {1, 2, 3, 4, 5, 4, 3, 2, 1};
 
    // size of the array
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // count number of occurrences of 4 in the array
    int count1 = count(arr, arr + n, 4);
 
    cout << "Number of occurrences of 4 in the array: " << count1 << endl;
 
 
    //implementation of count in vector
    vector<int> vec{1, 2, 3, 4, 5, 5, 5, 5, 6, 7, 8, 9, 10};
 
    // Count the number of occurrences of the element 5
    int count2 = count(vec.begin(), vec.end(), 5);
 
    cout << "Number of occurrences of 5 in vector: " << count2 <<endl;
 
 
    return 0;
}
 
//This code is contributed by akashjha412


Output

Number of occurrences of 4 in the array: 2
Number of occurrences of 5 in vector: 4

Find

Find can be done with help of find() function, find (start index , last index , element). This function is used to search for a specific element in an array and a vector. Example:

C++




// C++ program to find element in Array and vector
// using find() function
 
#include <bits/stdc++.h>
using namespace std;
 
int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int size = sizeof(arr) / sizeof(arr[0]);
    int key = 3;
 
    // Find the element in the array
    int* ptr = find(arr, arr + size, key);
 
    // If the element is found
    if (ptr != arr + size) {
        cout << "Element found in array at position "
                  << (ptr - arr) << endl;
    }
   // If the element is not found
    else {
        cout << "Element not found in array" << endl;
    }
 
    // Implementation of find() in vector
 
    // Create a vector of integers
    vector<int> vec = {1, 2, 3, 4, 5};
 
    // Find the first occurrence of 4 in the vector
    auto it = find(vec.begin(), vec.end(), 4);
 
    // If the element is found
    if (it != vec.end())
    {
        cout << "Element found in vector at index " << distance(vec.begin(), it) << endl;
    }
    // If the element is not found
    else
    {
        cout << "Element not found in vector" << endl;
    }
 
    return 0;
}
 
//This code is contributed by akashjha412


Output

Element found in array at position 2
Element found in vector at index 2


Last Updated : 13 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads