forked from Ananya02850/HacktoberFest-2022
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVector.cpp
More file actions
58 lines (44 loc) · 1.26 KB
/
Copy pathVector.cpp
File metadata and controls
58 lines (44 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// A C++ program to demonstrate working of sort(),
// reverse()
#include <algorithm>
#include <iostream>
#include <vector>
#include <numeric> //For accumulate operation
using namespace std;
int main()
{
// Initializing vector with array values
int arr[] = {10, 20, 5, 23 ,42 , 15};
int n = sizeof(arr)/sizeof(arr[0]);
cout << "Size of Array : " << n <<endl;
vector<int> vect(arr, arr+n);
// Vector before sorting
cout << "Vector is : ";
for(int i=0;i<n;i++){
cout << vect[i] << " ";
}
// sorting the vector in ascending order
sort(vect.begin(), vect.end());
cout << "\nVector after sorting : ";
for(int i=0;i<n;i++) {
cout << vect[i] << " ";
}
// Reversing the Vector
reverse(vect.begin(), vect.end());
cout << "\nVector after Reversing : ";
for(int i=0;i<n;i++) {
cout << vect[i] << " ";
}
// Maximum element of vectoe
cout << "\nMaximum element of Vector : ";
cout << *max_element(vect.begin(), vect.end());
// Minimum element of Vecotr
cout << "\nMinimum element of Vector : ";
cout << *min_element(vect.begin(), vect.end());
// Summation of vector
cout << "\nThe summation of vector element is : ";
// cout << accumulate(vect.begin(), vect.end(), 0);
int val = accumulate(vect.begin(), vect.end(), 0);
cout<<val;
return 0;
}