-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint-evens.cpp
More file actions
44 lines (36 loc) · 870 Bytes
/
print-evens.cpp
File metadata and controls
44 lines (36 loc) · 870 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
/* testing out vectors... */
#include <vector>
using std::vector;
#include <string>
using std::string;
#include <iostream>
using std::cin;
using std::cout;
/* write a function that takes a vector of integers as input and
* returns another vector containing only the *even* elements. */
vector<int> evens(vector<int> V)
{
vector<int> output; /* we'll return this vector */
for (size_t i = 0; i < V.size(); i++) {
if (V[i] % 2 == 0) {
output.push_back(V[i]);
}
}
return output;
}
/* NOTE: a better prototype for the above would have been this:
* vector<int> evens(const vector<int>& V);
* TODO: can you guess why? (Hint: it is about efficiency...)
* */
int main()
{
vector<int> test;
for (int i = 0; i < 11; i++) {
test.push_back(i);
}
test = evens(test);
for (size_t i = 0; i < test.size(); i++) {
cout << test[i] << " ";
}
return 0;
}