-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvectors.cpp
More file actions
52 lines (47 loc) · 1.75 KB
/
vectors.cpp
File metadata and controls
52 lines (47 loc) · 1.75 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
/* testing out vectors... */
#include <vector>
using std::vector;
#include <string>
using std::string;
#include <iostream>
using std::cin;
using std::cout;
int main()
{
/* read strings from stdin and print them in reverse order. */
string s; /* for input */
vector<string> V; /* each V[i] will be of type string */
while (cin >> s) {
V.push_back(s);
}
/* now print in reverse order: */
for (size_t i = V.size()-1; i != (size_t)-1; i--) {
cout << V[i] << " ";
}
cout << "\n";
return 0;
}
/* TODO: write a function that takes a vector and searches for
* a particular value x, returning true if and only if x is found. */
/* TODO: write a function that takes a vector V of integers which
* is *sorted*, and produces a vector of the unique integers from V.
* E.g., if V = {1,2,2,3,3,3,4}, then the result should
* be {1,2,3,4}. Ideally, modify the vector *in-place*.
* That is, modify the vector V that is given to the function directly,
* and don't create any new vectors. As a warm up: return a new vector with
* the
* result.
* */
/* TODO: write a *binary search* on a sorted vector. The idea is to
* kind of emulate the process you use to find a particular page in a book:
* 1. open the book to some page in the middle.
* 2. if the page number was too high, open to the middle of the pages to the
* left; if it was too low, open to the middle of the pages to the right
* 3. continue as above until you found the right page.
*
* This might be a little challenging. Ask questions if you get stuck.
* */
/* TODO: write a function that takes a vector and places the elements
* in sorted order. Warning: this could be kind of challenging. There is
* a solution in l3.pdf, but you should try to come up with something on
* your own first if possible. */