Related Articles
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 ` `using` `namespace` `std; ` ` `  `int` `main() ` `{ ` `    ``int` `a[] = { 1, 7, 2, 4, 8, 3 }; ` `    ``int` `l = ``sizeof``(a) / ``sizeof``(a); ` `    ``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 ` `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 ` `using` `namespace` `std; ` ` `  `int` `main() ` `{ ` `    ``int` `a[] = { 1, 7, 2, 4, 8, 3 }; ` `    ``int` `l = ``sizeof``(a) / ``sizeof``(a); ` `    ``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 ` `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 ` `using` `namespace` `std; ` ` `  `int` `main() ` `{ ` `    ``int` `a[] = { 1, 7, 2, 4, 8, 3 }; ` `    ``int` `l = ``sizeof``(a) / ``sizeof``(a); ` `    ``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 ` `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
```

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :