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 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; } |
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; } |
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; } |
sum of array: 25 Maximum element in array: 8 Minimum element in array: 1
Recommended Posts:
- How to join two Vectors using STL in C++?
- Quickly check if two STL vectors contain same elements or not
- Difference between std::remove and vector::erase for vectors
- Check whether an array can be fit into another array rearranging the elements in the array
- Generate original array from an array that store the counts of greater elements on right
- Sort an array where a subarray of a sorted array is in reverse order
- First string from the given array whose reverse is also present in the same array
- array::front() and array::back() in C++ STL
- Array with GCD of any of its subset belongs to the given array
- array::rbegin() and array::rend() in C++ STL
- array::cbegin() and array::cend() in C++ STL
- array::fill() and array::swap() in C++ STL
- array::crbegin() and array::crend() in C++ STL
- Sorting an array according to another array using pair in STL
- What’s difference between “array” and “&array” for “int array[5]” ?
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.



