Skip to content

Commit 9eccc57

Browse files
author
Jaege
committed
Add soultions to second half of exercises in chapter 14.
1 parent b02929b commit 9eccc57

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+3244
-5
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,11 @@ This repo is the solutions to exercises in book [_C++ Primer_ (5th Edition)](htt
132132

133133
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10
134134
--:|--:|--:|--:|--:|--:|--:|--:|--:|--:
135-
[1](ch14/14.1.cpp)|[2](ch14/14.2.cpp)|[3](ch14/14.3.cpp)|[4](ch14/14.4.cpp)|[5](ch14/14.5.cpp)|[6](ch14/14.6.cpp)|[7](ch14/14.7.cpp)|[8](ch14/14.8.cpp)|[9](ch14/14.9.cpp)|[10](ch14/14.10.cpp)|
136-
[11](ch14/14.11.cpp)|[12](ch14/14.12.cpp)|[13](ch14/14.13.cpp)|[14](ch14/14.14.cpp)|[15](ch14/14.15.cpp)|[16](ch14/14.16.cpp)|[17](ch14/14.17.cpp)|[18](ch14/14.18.cpp)|[19](ch14/14.19.cpp)|[20](ch14/14.20.cpp)|
137-
[21](ch14/14.21.cpp)|[22](ch14/14.22.cpp)|[23](ch14/14.23.cpp)|[24](ch14/14.24.cpp)|[25](ch14/14.25.cpp)|[26](ch14/14.26.cpp)|[27](ch14/14.27.cpp)|[28](ch14/14.28.cpp)|[29](ch14/14.29.cpp)|[30](ch14/14.30.cpp)|
138-
[31](ch14/14.31.cpp)|[32](ch14/14.32.cpp)|[33](ch14/14.33.cpp)|[34](ch14/14.34.cpp)|[35](ch14/14.35.cpp)|[36](ch14/14.36.cpp)|[37](ch14/14.37.cpp)|[38](ch14/14.38.cpp)|[39](ch14/14.39.cpp)|[40](ch14/14.40.cpp)|
139-
[41](ch14/14.41.cpp)|[42](ch14/14.42.cpp)|[43](ch14/14.43.cpp)|[44](ch14/14.44.cpp)|[45](ch14/14.45.cpp)|[46](ch14/14.46.cpp)|[47](ch14/14.47.cpp)|[48](ch14/14.48.cpp)|[49](ch14/14.49.cpp)|[50](ch14/14.50.cpp)|
135+
[1](ch14/14.1.md)|[2](ch14/14.2.cpp)|[3](ch14/14.3.md)|[4](ch14/14.4.md)|[5](ch14/14.5.md)|[6](ch14/14.6.cpp)|[7](ch14/14.7)|[8](ch14/14.8.md)|[9](ch14/14.9.cpp)|[10](ch14/14.10.md)|
136+
[11](ch14/14.11.md)|[12](ch14/14.12.md)|[13](ch14/14.13.cpp)|[14](ch14/14.14.md)|[15](ch14/14.15.md)|[16](ch14/14.16)|[17](ch14/14.17.md)|[18](ch14/14.18)|[19](ch14/14.19.md)|[20](ch14/14.20.md)|
137+
[21](ch14/14.21.cpp)|[22](ch14/14.22.cpp)|[23](ch14/14.23)|[24](ch14/14.24.md)|[25](ch14/14.25.md)|[26](ch14/14.26)|[27](ch14/14.27)|[28](ch14/14.28)|[29](ch14/14.29.md)|[30](ch14/14.30)|
138+
[31](ch14/14.31.md)|[32](ch14/14.32)|[33](ch14/14.33.md)|[34](ch14/14.34.cpp)|[35](ch14/14.35.cpp)|[36](ch14/14.36.md)|[37](ch14/14.37.cpp)|[38](ch14/14.38.cpp)|[39](ch14/14.39.cpp)|[40](ch14/14.40.cpp)|
139+
[41](ch14/14.41.md)|[42](ch14/14.42.cpp)|[43](ch14/14.43.cpp)|[44](ch14/14.44.cpp)|[45](ch14/14.45.cpp)|[46](ch14/14.46.md)|[47](ch14/14.47.md)|[48](ch14/14.48.md)|[49](ch14/14.49.md)|[50](ch14/14.50.cpp)|
140140
[51](ch14/14.51.cpp)|[52](ch14/14.52.cpp)|[53](ch14/14.53.cpp)
141141

142142
<!---

ch14/14.27/ConstStrBlobPtr.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include "ConstStrBlobPtr.h"
2+
#include "StrBlob.h"
3+
4+
ConstStrBlobPtr::ConstStrBlobPtr() : wptr(), curr(0) {}
5+
ConstStrBlobPtr::ConstStrBlobPtr(const StrBlob &sb, size_type pos)
6+
: wptr(sb.data), curr(pos) {}
7+
8+
std::shared_ptr<std::vector<std::string>>
9+
ConstStrBlobPtr::check(size_type pos, const std::string &msg) const {
10+
auto ret = wptr.lock();
11+
if (!ret)
12+
throw std::runtime_error("unbound ConstStrBlobPtr");
13+
if (pos >= ret->size())
14+
throw std::out_of_range(msg);
15+
return ret;
16+
}
17+
18+
const std::string &ConstStrBlobPtr::deref() const {
19+
auto sp = check(curr, "deference past end of ConstStrBlobPtr");
20+
return (*sp)[curr];
21+
}
22+
23+
//ConstStrBlobPtr &ConstStrBlobPtr::inc() {
24+
// check(curr, "increment past end of ConstStrBlobPtr");
25+
// ++curr;
26+
// return *this;
27+
//}
28+
29+
ConstStrBlobPtr &ConstStrBlobPtr::operator++() {
30+
check(curr, "increment past end of ConstStrBlobPtr");
31+
++curr;
32+
return *this;
33+
}
34+
35+
ConstStrBlobPtr ConstStrBlobPtr::operator++(int) {
36+
auto ret = *this;
37+
++*this;
38+
return ret;
39+
}
40+
41+
ConstStrBlobPtr &ConstStrBlobPtr::operator--() {
42+
--curr;
43+
check(curr, "decrement past begin of ConstStrBlobPtr");
44+
return *this;
45+
}
46+
47+
ConstStrBlobPtr ConstStrBlobPtr::operator--(int) {
48+
auto ret = *this;
49+
--*this;
50+
return ret;
51+
}
52+
53+
bool operator==(const ConstStrBlobPtr &lhs, const ConstStrBlobPtr &rhs) {
54+
// compare identity
55+
return lhs.wptr.lock() == rhs.wptr.lock() && lhs.curr == rhs.curr;
56+
}
57+
58+
bool operator!=(const ConstStrBlobPtr &lhs, const ConstStrBlobPtr &rhs) {
59+
return !(lhs == rhs);
60+
}
61+
62+
bool operator<(const ConstStrBlobPtr &lhs, const ConstStrBlobPtr &rhs) {
63+
return lhs.wptr.lock() == rhs.wptr.lock() && lhs.curr < rhs.curr;
64+
}
65+
66+
bool operator>(const ConstStrBlobPtr &lhs, const ConstStrBlobPtr &rhs) {
67+
return rhs < lhs;
68+
}
69+
70+
bool operator<=(const ConstStrBlobPtr &lhs, const ConstStrBlobPtr &rhs) {
71+
return !(lhs > rhs);
72+
}
73+
74+
bool operator>=(const ConstStrBlobPtr &lhs, const ConstStrBlobPtr &rhs) {
75+
return !(lhs < rhs);
76+
}

ch14/14.27/ConstStrBlobPtr.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#ifndef CONSTSTRBLOBPTR_H
2+
#define CONSTSTRBLOBPTR_H
3+
4+
class StrBlob;
5+
6+
#include <vector>
7+
#include <string>
8+
#include <memory>
9+
10+
class ConstStrBlobPtr {
11+
friend bool operator==(const ConstStrBlobPtr &, const ConstStrBlobPtr &);
12+
friend bool operator!=(const ConstStrBlobPtr &, const ConstStrBlobPtr &);
13+
friend bool operator<(const ConstStrBlobPtr &, const ConstStrBlobPtr &);
14+
friend bool operator>(const ConstStrBlobPtr &, const ConstStrBlobPtr &);
15+
friend bool operator<=(const ConstStrBlobPtr &, const ConstStrBlobPtr &);
16+
friend bool operator>=(const ConstStrBlobPtr &, const ConstStrBlobPtr &);
17+
public:
18+
typedef std::vector<std::string>::size_type size_type;
19+
20+
ConstStrBlobPtr();
21+
explicit ConstStrBlobPtr(const StrBlob &sb, size_type pos = 0);
22+
23+
// do not check range
24+
const std::string &operator[](size_type n) const { return (*wptr.lock())[n]; }
25+
26+
const std::string &deref() const;
27+
//ConstStrBlobPtr &inc();
28+
29+
ConstStrBlobPtr &operator++();
30+
ConstStrBlobPtr operator++(int);
31+
ConstStrBlobPtr &operator--();
32+
ConstStrBlobPtr operator--(int);
33+
34+
private:
35+
std::weak_ptr<std::vector<std::string>> wptr;
36+
size_type curr;
37+
38+
std::shared_ptr<std::vector<std::string>>
39+
check(size_type pos, const std::string &msg) const;
40+
};
41+
42+
bool operator==(const ConstStrBlobPtr &, const ConstStrBlobPtr &);
43+
bool operator!=(const ConstStrBlobPtr &, const ConstStrBlobPtr &);
44+
bool operator<(const ConstStrBlobPtr &, const ConstStrBlobPtr &);
45+
bool operator>(const ConstStrBlobPtr &, const ConstStrBlobPtr &);
46+
bool operator<=(const ConstStrBlobPtr &, const ConstStrBlobPtr &);
47+
bool operator>=(const ConstStrBlobPtr &, const ConstStrBlobPtr &);
48+
49+
#endif

ch14/14.27/StrBlob.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include "StrBlob.h"
2+
#include "StrBlobPtr.h"
3+
#include "ConstStrBlobPtr.h"
4+
5+
StrBlob::StrBlob() : data(std::make_shared<std::vector<std::string>>()) {}
6+
StrBlob::StrBlob(std::initializer_list<std::string> il)
7+
: data(std::make_shared<std::vector<std::string>>(il)) {}
8+
StrBlob::StrBlob(const StrBlob &sb)
9+
: data(std::make_shared<std::vector<std::string>>(*sb.data)) {}
10+
11+
StrBlob &StrBlob::operator=(const StrBlob &sb) {
12+
data = std::make_shared<std::vector<std::string>>(*sb.data);
13+
return *this;
14+
}
15+
16+
void StrBlob::check(size_type pos, const std::string &msg) const {
17+
if (pos >= data->size())
18+
throw std::out_of_range(msg);
19+
}
20+
21+
void StrBlob::pop_back() {
22+
check(0, "pop_back on empty StrBlob");
23+
data->pop_back();
24+
}
25+
26+
std::string &StrBlob::front() {
27+
check(0, "front on empty StrBlob");
28+
return data->front();
29+
}
30+
31+
const std::string &StrBlob::front() const {
32+
check(0, "front on empty StrBlob");
33+
return data->front();
34+
}
35+
36+
std::string &StrBlob::back() {
37+
check(0, "back on empty StrBlob");
38+
return data->back();
39+
}
40+
41+
const std::string &StrBlob::back() const {
42+
check(0, "back on empty StrBlob");
43+
return data->back();
44+
}
45+
46+
StrBlobPtr StrBlob::begin() {
47+
return StrBlobPtr(*this);
48+
}
49+
50+
StrBlobPtr StrBlob::end() {
51+
return StrBlobPtr(*this, data->size());
52+
}
53+
54+
ConstStrBlobPtr StrBlob::cbegin() const {
55+
return ConstStrBlobPtr(*this);
56+
}
57+
58+
ConstStrBlobPtr StrBlob::cend() const {
59+
return ConstStrBlobPtr(*this, data->size());
60+
}
61+
62+
bool operator==(const StrBlob &lhs, const StrBlob &rhs) {
63+
//return lhs.data == rhs.data; // compare identity(address)
64+
return *lhs.data == *rhs.data; // compare value
65+
}
66+
67+
bool operator!=(const StrBlob &lhs, const StrBlob &rhs) {
68+
return !(lhs == rhs);
69+
}
70+
71+
bool operator<(const StrBlob &lhs, const StrBlob &rhs) {
72+
return *lhs.data < *rhs.data; // compare value
73+
}
74+
75+
bool operator>(const StrBlob &lhs, const StrBlob &rhs) {
76+
return rhs < lhs;
77+
}
78+
79+
bool operator<=(const StrBlob &lhs, const StrBlob &rhs) {
80+
return !(lhs > rhs);
81+
}
82+
83+
bool operator>=(const StrBlob &lhs, const StrBlob &rhs) {
84+
return !(lhs < rhs);
85+
}

ch14/14.27/StrBlob.h

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#ifndef STRBLOB_H
2+
#define STRBLOB_H
3+
4+
class StrBlobPtr;
5+
class ConstStrBlobPtr;
6+
7+
#include <vector>
8+
#include <string>
9+
#include <initializer_list>
10+
#include <memory>
11+
#include <iostream>
12+
13+
class StrBlob {
14+
friend class StrBlobPtr;
15+
friend class ConstStrBlobPtr;
16+
friend bool operator==(const StrBlob &, const StrBlob &);
17+
friend bool operator!=(const StrBlob &, const StrBlob &);
18+
friend bool operator<(const StrBlob &, const StrBlob &);
19+
friend bool operator>(const StrBlob &, const StrBlob &);
20+
friend bool operator<=(const StrBlob &, const StrBlob &);
21+
friend bool operator>=(const StrBlob &, const StrBlob &);
22+
public:
23+
typedef std::vector<std::string>::size_type size_type;
24+
25+
StrBlob();
26+
StrBlob(std::initializer_list<std::string> il);
27+
28+
StrBlob(const StrBlob &);
29+
StrBlob &operator=(const StrBlob &);
30+
31+
// do not check range
32+
std::string &operator[](size_type n) { return (*data)[n]; }
33+
const std::string &operator[](size_type n) const { return (*data)[n]; }
34+
35+
size_type size() const { return data->size(); }
36+
bool empty() const { return data->empty(); }
37+
38+
void push_back(const std::string &s);
39+
void push_back(std::string &&s);
40+
void pop_back();
41+
42+
std::string &front();
43+
const std::string &front() const;
44+
std::string &back();
45+
const std::string &back() const;
46+
47+
StrBlobPtr begin();
48+
StrBlobPtr end();
49+
50+
ConstStrBlobPtr cbegin() const;
51+
ConstStrBlobPtr cend() const;
52+
53+
private:
54+
std::shared_ptr<std::vector<std::string>> data;
55+
56+
void check(size_type pos, const std::string &msg) const;
57+
};
58+
59+
bool operator==(const StrBlob &, const StrBlob &);
60+
bool operator!=(const StrBlob &, const StrBlob &);
61+
bool operator<(const StrBlob &, const StrBlob &);
62+
bool operator>(const StrBlob &, const StrBlob &);
63+
bool operator<=(const StrBlob &, const StrBlob &);
64+
bool operator>=(const StrBlob &, const StrBlob &);
65+
66+
inline void StrBlob::push_back(const std::string &s) {
67+
data->push_back(s);
68+
}
69+
70+
inline void StrBlob::push_back(std::string &&s) {
71+
std::cout << "StrBlob::push_back(std::string &&s)" << std::endl;
72+
data->push_back(std::move(s));
73+
}
74+
75+
#endif

ch14/14.27/StrBlobPtr.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include "StrBlobPtr.h"
2+
#include "StrBlob.h"
3+
4+
StrBlobPtr::StrBlobPtr() : wptr(), curr(0) {}
5+
StrBlobPtr::StrBlobPtr(StrBlob &sb, size_type pos)
6+
: wptr(sb.data), curr(pos) {}
7+
8+
std::shared_ptr<std::vector<std::string>>
9+
StrBlobPtr::check(size_type pos, const std::string &msg) const {
10+
auto ret = wptr.lock();
11+
if (!ret)
12+
throw std::runtime_error("unbound StrBlobPtr");
13+
if (pos >= ret->size())
14+
throw std::out_of_range(msg);
15+
return ret;
16+
}
17+
18+
std::string &StrBlobPtr::deref() const {
19+
auto sp = check(curr, "deference past end of StrBlobPtr");
20+
return (*sp)[curr];
21+
}
22+
23+
//StrBlobPtr &StrBlobPtr::inc() {
24+
// check(curr, "increment past end of StrBlobPtr");
25+
// ++curr;
26+
// return *this;
27+
//}
28+
29+
StrBlobPtr &StrBlobPtr::operator++() {
30+
check(curr, "increment past end of StrBlobPtr");
31+
++curr;
32+
return *this;
33+
}
34+
35+
StrBlobPtr StrBlobPtr::operator++(int) {
36+
auto ret = *this;
37+
++*this;
38+
return ret;
39+
}
40+
41+
StrBlobPtr &StrBlobPtr::operator--() {
42+
--curr;
43+
check(curr, "decrement past begin of StrBlobPtr");
44+
return *this;
45+
}
46+
47+
StrBlobPtr StrBlobPtr::operator--(int) {
48+
auto ret = *this;
49+
--*this;
50+
return ret;
51+
}
52+
53+
bool operator==(const StrBlobPtr &lhs, const StrBlobPtr &rhs) {
54+
// compare identity
55+
return lhs.wptr.lock() == rhs.wptr.lock() && lhs.curr == rhs.curr;
56+
}
57+
58+
bool operator!=(const StrBlobPtr &lhs, const StrBlobPtr &rhs) {
59+
return !(lhs == rhs);
60+
}
61+
62+
bool operator<(const StrBlobPtr &lhs, const StrBlobPtr &rhs) {
63+
return lhs.wptr.lock() == rhs.wptr.lock() && lhs.curr < rhs.curr;
64+
}
65+
66+
bool operator>(const StrBlobPtr &lhs, const StrBlobPtr &rhs) {
67+
return rhs < lhs;
68+
}
69+
70+
bool operator<=(const StrBlobPtr &lhs, const StrBlobPtr &rhs) {
71+
return !(lhs > rhs);
72+
}
73+
74+
bool operator>=(const StrBlobPtr &lhs, const StrBlobPtr &rhs) {
75+
return !(lhs < rhs);
76+
}

0 commit comments

Comments
 (0)