-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_algos.cpp
More file actions
53 lines (41 loc) · 953 Bytes
/
vector_algos.cpp
File metadata and controls
53 lines (41 loc) · 953 Bytes
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
#include "vector_algos.hpp"
#include <iostream>
#include <string>
// Function that reads integer input from the user until they press Ctrl+Z. Returns a vector with the ints
std::vector<int> read_int_vector()
{
int x = 0;
std::vector<int> input;
std::cout << "Enter as many numbers as you like.\n";
while (true)
{
while (std::cin >> x)
input.push_back(x);
if (std::cin.eof())
break;
std::string s;
std::cin.clear();
std::getline(std::cin, s);
std::cout << "Warning: Ignoring " << s << "\n";
}
return input;
}
// Returns an int corresponding to the sum of elements in a vector
int sum(std::vector<int> v)
{
int sum = 0;
for (auto e : v)
sum += e;
return sum;
}
// Returns a vector containing the ints which are greater than x inside vector v
std::vector<int> filter_greater_than(std::vector<int> v, int x)
{
std::vector<int> result;
for (auto e : v)
{
if (e > x)
result.push_back(e);
}
return result;
}