-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_STL_algorithms.cpp
More file actions
59 lines (45 loc) · 1.74 KB
/
Copy pathvector_STL_algorithms.cpp
File metadata and controls
59 lines (45 loc) · 1.74 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
59
/*
Demonstrate operations on vectors and algorithms to use on them
~$ g++ vector_STL_algorithms.cpp -o vec_algos
*/
#include <iostream> // std::cout
#include <algorithm> // std::generate, std::reverse, std::count, std::find
#include <vector> // std::vector
#include <cstdlib> // std::rand, std::srand
#include <numeric> // accumulate operation
using std::cout;
using std::endl;
// function generator:
int RandomNumber(){ return (std::rand()%100); }
int main() {
// initialize a vector
std::srand(97);
std::vector<int> aVec (8);
std::generate(aVec.begin(), aVec.end(), RandomNumber);
cout << "our vector:";
for (std::vector<int>::iterator it=aVec.begin(); it!=aVec.end(); ++it)
cout << ' ' << *it;
cout << '\n';
// reverse the vector
std::reverse(aVec.begin(), aVec.end());
cout << "reversed vector:";
for (std::vector<int>::iterator it=aVec.begin(); it!=aVec.end(); ++it)
cout << ' ' << *it;
cout << '\n';
// maximum element
cout << "max element: " << *max_element(aVec.begin(), aVec.end()) << endl;
// minimum element
cout << "min element: " << *min_element(aVec.begin(), aVec.end()) << endl;
// sum
cout << "max element: " << accumulate(aVec.begin(), aVec.end(), 0) << endl;
// count the occurances of a number, say 55
cout << "nb occurances of 55: " << std::count(aVec.begin(), aVec.end(), 55) << endl;
// find the last occurance of the number 55
std::vector<int>::iterator it;
it = std::find(aVec.begin(), aVec.end(), 55);
if (it != aVec.end())
std::cout << "Element found at: " << it-aVec.begin() << endl;
else
std::cout << "Element not found in vector" << endl;
return 0;
}