-
Notifications
You must be signed in to change notification settings - Fork 1
/
12-0.cpp
100 lines (79 loc) · 1.99 KB
/
12-0.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#include "11-0.h"
using std::istream;
using std::ostream;
using std::back_inserter;
using std::copy;
using std::strlen;
using std::cout;
using std::cin;
using std::endl;
class Str {
friend std::istream& operator>>(istream&, Str&);
public:
// as before
typedef Vec<char>::size_type size_type;
Str() { }
Str(size_type n, char c): data(n, c) { }
Str(const char* cp) {
std::copy(cp, cp + strlen(cp), back_inserter(data));
}
template <class In> Str(In b, In e) {
std::copy(b, e, std::back_inserter(data));
}
char& operator[](size_type i) { return data[i]; }
const char& operator[] (size_type i) const { return data[i]; }
size_type size() const { return data.size(); }
Str& operator+=(const Str& s) {
copy(s.data.begin(), s.data.end(), back_inserter(data));
return *this;
}
private:
Vec<char> data;
};
Str operator+(const Str& s, const Str& t)
{
Str r = s;
r += t;
return r;
}
ostream& operator<<(ostream& os, const Str& s) {
for (Str::size_type i = 0; i != s.size(); i++)
os << s[i];
return os;
}
istream& operator>>(istream& is, Str& s)
{
// obliterate existing value(s)
s.data.uncreate();
// read and discard leading whitespace
char c;
while (is.get(c) && isspace(c)) ; // nothing to do
// if still something to read, do so until next whitespace character
if (is) {
do s.data.push_back(c);
while (is.get(c) && !isspace(c));
// if we read whitespace, then put it back on the stream
if (is)
is.unget();
}
return is;
}
int main()
{
Str first;
Str last;
Str fullname;
cout << "Enter your first name: " << endl;
cin >> first;
cout << "Enter your last name: " << endl;
cin >> last;
fullname += first;
fullname += " ";
fullname += last;
cout << "Your full name is: " << fullname << endl;
return 0;
}