TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
vector_important_functions.cpp File Reference

A C++ program to demonstrate working of std::sort(), std::reverse() More...

#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
Include dependency graph for vector_important_functions.cpp:

Go to the source code of this file.

Functions

int main ()
 

Detailed Description

A C++ program to demonstrate working of std::sort(), std::reverse()

Definition in file vector_important_functions.cpp.

Function Documentation

◆ main()

int main ( void )

Main function

Definition at line 11 of file vector_important_functions.cpp.

11 {
12 // Initializing vector with array values
13 int arr[] = {10, 20, 5, 23, 42, 15};
14 int n = sizeof(arr) / sizeof(arr[0]);
15 std::vector<int> vect(arr, arr + n);
16
17 std::cout << "Vector is: ";
18 for (int i = 0; i < n; i++) std::cout << vect[i] << " ";
19
20 // Sorting the Vector in Ascending order
21 std::sort(vect.begin(), vect.end());
22
23 std::cout << "\nVector after sorting is: ";
24 for (int i = 0; i < n; i++) std::cout << vect[i] << " ";
25
26 // Reversing the Vector
27 std::reverse(vect.begin(), vect.end());
28
29 std::cout << "\nVector after reversing is: ";
30 for (int i = 0; i < 6; i++) std::cout << vect[i] << " ";
31
32 std::cout << "\nMaximum element of vector is: ";
33 std::cout << *max_element(vect.begin(), vect.end());
34
35 std::cout << "\nMinimum element of vector is: ";
36 std::cout << *min_element(vect.begin(), vect.end());
37
38 // Starting the summation from 0
39 std::cout << "\nThe summation of vector elements is: ";
40 std::cout << accumulate(vect.begin(), vect.end(), 0);
41
42 return 0;
43}