-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsection5.4.2.cpp
52 lines (51 loc) · 1.29 KB
/
section5.4.2.cpp
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
#include <iostream>
#include <vector>
#include <stdexcept>
using std::vector;
using std::cin; using std::cout; using std::endl;
using std::invalid_argument;
bool prefix_or_not(vector<int> one, vector<int> two) {
bool first_try=true;
vector<int> three, four;
auto iter1 = one.begin(), iter2 = two.begin();
try {
if (iter1 == one.end() || iter2 == two.end()) {
throw invalid_argument("invalid input-empty vector");
}
} catch (invalid_argument x) {
int new_vec;
cout<<"please enter new vector values, ended with a 69."<<endl<<"vector one:";
while(cin>>new_vec && new_vec!=69) {
three.push_back(new_vec);
}
cout<<endl<<"vector two:";
while(cin>>new_vec && new_vec!=69) {
four.push_back(new_vec);
}
iter1 = three.begin(),iter2=four.begin();
first_try=false;
}
for (; *iter1 == *iter2; ++iter1, ++iter2) {
if (first_try){
if (iter1+1==one.end()) {
cout<<"vector one is a prefix of vector 2";
return true;
}
if (iter2+1==two.end()) {
cout<<"vector two is a prefix of vector 1";
return true;
}
} else {
if (iter1+1==three.end()) {
cout<<"vector one is a prefix of vector 2";
return true;
}
if (iter2+1==four.end()) {
cout<<"vector two is a prefix of vector 1";
return true;
}
}
}
cout<<"neither vector is a prefix of the other";
return false;
}