Working with Array and Vectors using STL in C++
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
#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
#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;
}
|
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
#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
#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;
}
|
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
#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
#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++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = {1, 2, 3, 4, 5, 4, 3, 2, 1};
int n = sizeof (arr) / sizeof (arr[0]);
int count1 = count(arr, arr + n, 4);
cout << "Number of occurrences of 4 in the array: " << count1 << endl;
vector< int > vec{1, 2, 3, 4, 5, 5, 5, 5, 6, 7, 8, 9, 10};
int count2 = count(vec.begin(), vec.end(), 5);
cout << "Number of occurrences of 5 in vector: " << count2 <<endl;
return 0;
}
|
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++
#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;
int * ptr = find(arr, arr + size, key);
if (ptr != arr + size) {
cout << "Element found in array at position "
<< (ptr - arr) << endl;
}
else {
cout << "Element not found in array" << endl;
}
vector< int > vec = {1, 2, 3, 4, 5};
auto it = find(vec.begin(), vec.end(), 4);
if (it != vec.end())
{
cout << "Element found in vector at index " << distance(vec.begin(), it) << endl;
}
else
{
cout << "Element not found in vector" << endl;
}
return 0;
}
|
Output
Element found in array at position 2
Element found in vector at index 2
Last Updated :
13 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...