-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq5.cpp
More file actions
43 lines (33 loc) · 769 Bytes
/
q5.cpp
File metadata and controls
43 lines (33 loc) · 769 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
#include <iostream>
#include <vector>
using namespace std;
void print(const string& label, const vector<int>& v)
{
cout << label << "( ";
for (int i = 0; i < v.size(); ++i) cout << v[i] << ' ';
cout << ")\n";
}
void rev1(vector<int>& v)
{
vector<int> v2;
for (int i = v.size() - 1; 0 <= i; --i)
v2.push_back(v[i]);
v = v2;
}
void rev2(vector<int>& v)
{
for (int i = 0; i < v.size() / 2; ++i)
swap(v[i], v[v.size() - 1 - i]);
}
int main()
{
vector<int> val;
cout << "Please enter a sequence of integers ending with any non-digit character: ";
int i;
while (cin >> i) val.push_back(i);
print("String: ", val);
rev1(val);
print("First reverse: ", val);
rev2(val);
print("Second reverse: ", val);
}