Skip to content

Commit

Permalink
refactor 14.5
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Mar 26, 2015
1 parent 71895a5 commit 5992e78
Show file tree
Hide file tree
Showing 12 changed files with 1,086 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
##C++ Primer 5 Answers

[![](https://img.shields.io/github/issues/pezy/Cpp-Primer.svg)](https://github.com/pezy/Cpp-Primer/issues)
[![](https://img.shields.io/github/issues/pezy/CppPrimer.svg)](https://github.com/pezy/CppPrimer/issues)
[![](https://img.shields.io/badge/license-CC0-blue.svg)](http://creativecommons.org/about/cc0)
[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/pezy/Cpp-Primer?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![](https://img.shields.io/badge/%E4%B8%AD%E6%96%87-%E8%AE%A8%E8%AE%BA%E5%8C%BA-yellowgreen.svg)](https://github.com/Ocxs/Issues)
[![](https://img.shields.io/badge/douban-%E5%B0%8F%E7%BB%84-green.svg)](http://www.douban.com/group/532124/)
[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/pezy/Cpp-Primer?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

### Note

Expand All @@ -18,7 +18,7 @@
- Please **fork**([How?](https://help.github.com/articles/fork-a-repo)) this repository first.
- **Commit**([How?](https://help.github.com/articles/create-a-repo#commit-your-first-change)) in your own repository.
- Give me a **pull request**([How?](https://help.github.com/articles/using-pull-requests)).
- Syncing with([How?](https://help.github.com/articles/syncing-a-fork/)) the origin repository. This is very **important** because this repo updates frequently.
- **Syncing** with([How?](https://help.github.com/articles/syncing-a-fork/)) the origin repository. This is very **important** because this repo updates frequently.

### Table of Contents

Expand Down
7 changes: 7 additions & 0 deletions ch14/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,10 @@ Sales_data operator+(const Sales_data &lhs, const Sales_data &rhs)
>Implement any other assignment operators your class should define. Explain which types should be used as operands and why.
see [Exercise 14.24](#exercise-1424)

## Exercise 14.26:
>Define subscript operators for your `StrVec`, `String`, `StrBlob`, and `StrBlobPtr` classes.
- `StrBlob` & `StrBlobPtr`: [hpp](ex14_26_StrBlob.h) | [cpp](ex14_26_StrBlob.cpp) | [Test](ex14_26_StrBlobTest.cpp)
- `StrVec`: [hpp](ex14_26_StrVec.h) | [cpp](ex14_26_StrVec.cpp) | [Test](ex14_26_StrVecMain.cpp)
- `String`: [hpp](ex14_26_String.h) | [cpp](ex14_26_String.cpp) | [Test](ex14_26_StringMain.cpp)
158 changes: 158 additions & 0 deletions ch14/ex14_26_StrBlob.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
#include "ex14_26_StrBlob.h"
#include <algorithm>

//==================================================================
//
// StrBlob - operators
//
//==================================================================

bool operator==(const StrBlob &lhs, const StrBlob &rhs)
{
return *lhs.data == *rhs.data;
}

bool operator!=(const StrBlob &lhs, const StrBlob &rhs)
{
return !(lhs == rhs);
}

bool operator< (const StrBlob &lhs, const StrBlob &rhs)
{
return std::lexicographical_compare(lhs.data->begin(), lhs.data->end(), rhs.data->begin(), rhs.data->end());
}

bool operator> (const StrBlob &lhs, const StrBlob &rhs)
{
return rhs < lhs;
}

bool operator<=(const StrBlob &lhs, const StrBlob &rhs)
{
return !(rhs < lhs);
}

bool operator>=(const StrBlob &lhs, const StrBlob &rhs)
{
return !(lhs < rhs);
}

//================================================================
//
// StrBlobPtr - operators
//
//================================================================

bool operator==(const StrBlobPtr &lhs, const StrBlobPtr &rhs)
{
return lhs.curr == rhs.curr;
}

bool operator!=(const StrBlobPtr &lhs, const StrBlobPtr &rhs)
{
return !(lhs == rhs);
}

bool operator< (const StrBlobPtr &x, const StrBlobPtr &y)
{
return x.curr < y.curr;
}

bool operator> (const StrBlobPtr &x, const StrBlobPtr &y)
{
return x.curr > y.curr;
}

bool operator<=(const StrBlobPtr &x, const StrBlobPtr &y)
{
return x.curr <= y.curr;
}

bool operator>=(const StrBlobPtr &x, const StrBlobPtr &y)
{
return x.curr >= y.curr;
}

//================================================================
//
// ConstStrBlobPtr - operators
//
//================================================================

bool operator==(const ConstStrBlobPtr &lhs, const ConstStrBlobPtr &rhs)
{
return lhs.curr == rhs.curr;
}

bool operator!=(const ConstStrBlobPtr &lhs, const ConstStrBlobPtr &rhs)
{
return !(lhs == rhs);
}

bool operator< (const ConstStrBlobPtr &lhs, const ConstStrBlobPtr &rhs)
{
return lhs.curr < rhs.curr;
}

bool operator> (const ConstStrBlobPtr &lhs, const ConstStrBlobPtr &rhs)
{
return lhs.curr > rhs.curr;
}

bool operator<=(const ConstStrBlobPtr &lhs, const ConstStrBlobPtr &rhs)
{
return lhs.curr <= rhs.curr;
}

bool operator>=(const ConstStrBlobPtr &lhs, const ConstStrBlobPtr &rhs)
{
return lhs.curr >= rhs.curr;
}

//==================================================================
//
// copy assignment operator and move assignment operator.
//
//==================================================================

StrBlob& StrBlob::operator=(const StrBlob &lhs)
{
data = make_shared<vector<string>>(*lhs.data);
return *this;
}

StrBlob& StrBlob::operator=(StrBlob &&rhs) NOEXCEPT
{
if (this != &rhs) {
data = std::move(rhs.data);
rhs.data = nullptr;
}

return *this;
}

//==================================================================
//
// members
//
//==================================================================

StrBlobPtr StrBlob::begin()
{
return StrBlobPtr(*this);
}

StrBlobPtr StrBlob::end()
{
return StrBlobPtr(*this, data->size());
}

ConstStrBlobPtr StrBlob::cbegin() const
{
return ConstStrBlobPtr(*this);
}

ConstStrBlobPtr StrBlob::cend() const
{
return ConstStrBlobPtr(*this, data->size());
}
Loading

0 comments on commit 5992e78

Please sign in to comment.