12.19
@pezy
//
// ex12_19.h
// Exercise 12.19
//
// Created by pezy on 12/26/14.
//
// Define your own version of StrBlobPtr and
// update your StrBlob class with the appropriate friend declaration and begin and end members.
//
// @See ex12_02.h
#ifndef CP5_ex12_19_h
#define CP5_ex12_19_h
#include <vector>
#include <string>
#include <initializer_list>
#include <memory>
#include <stdexcept>
using std::vector; using std::string;
class StrBlobPtr;
class StrBlob {
public:
using size_type = vector<string>::size_type;
friend class StrBlobPtr;
StrBlobPtr begin();
StrBlobPtr end();
StrBlob():data(std::make_shared<vector<string>>()) { }
StrBlob(std::initializer_list<string> il):data(std::make_shared<vector<string>>(il)) { }
size_type size() const { return data->size(); }
bool empty() const { return data->empty(); }
void push_back(const string &t) { data->push_back(t); }
void pop_back() {
check(0, "pop_back on empty StrBlob");
data->pop_back();
}
std::string& front() {
check(0, "front on empty StrBlob");
return data->front();
}
std::string& back() {
check(0, "back on empty StrBlob");
return data->back();
}
const std::string& front() const {
check(0, "front on empty StrBlob");
return data->front();
}
const std::string& back() const {
check(0, "back on empty StrBlob");
return data->back();
}
private:
void check(size_type i, const string &msg) const {
if (i >= data->size()) throw std::out_of_range(msg);
}
private:
std::shared_ptr<vector<string>> data;
};
class StrBlobPtr {
public:
StrBlobPtr():curr(0) { }
StrBlobPtr(StrBlob &a, size_t sz = 0):wptr(a.data), curr(sz) { }
bool operator!=(const StrBlobPtr& p) { return p.curr != curr; }
string& deref() const {
auto p = check(curr, "dereference past end");
return (*p)[curr];
}
StrBlobPtr& incr() {
check(curr, "increment past end of StrBlobPtr");
++curr;
return *this;
}
private:
std::shared_ptr<vector<string>> check(size_t i, const string &msg) const {
auto ret = wptr.lock();
if (!ret) throw std::runtime_error("unbound StrBlobPtr");
if (i >= ret->size()) throw std::out_of_range(msg);
return ret;
}
std::weak_ptr<vector<string>> wptr;
size_t curr;
};
#endif
//
// ex12_19.cpp
// Exercise 12.19
//
// Created by pezy on 12/26/14.
//
// Define your own version of StrBlobPtr and
// update your StrBlob class with the appropriate friend declaration and begin and end members.
//
// @See ex12_19.h
#include "ex12_19.h"
StrBlobPtr StrBlob::begin()
{
return StrBlobPtr(*this);
}
StrBlobPtr StrBlob::end()
{
return StrBlobPtr(*this, data->size());
}
12.20
//
// ex12_20.cpp
// Exercise 12.20
//
// Created by pezy on 12/26/14.
//
// Write a program that reads an input file a line at a time into
// a StrBlob and uses a StrBlobPtr to print each element in that StrBlob.
#include "ex12_19.h"
#include <fstream>
#include <iostream>
int main()
{
std::ifstream ifs("../data/book.txt");
StrBlob blob;
for (std::string str; std::getline(ifs, str); )
blob.push_back(str);
for (StrBlobPtr pbeg(blob.begin()), pend(blob.end()); pbeg != pend; pbeg.incr())
std::cout << pbeg.deref() << std::endl;
}
12.21
前面的更好,更容易看明了,看懂
12.22
//
// ex12_22.cpp
// Exercise 12.22
//
// Created by pezy on 12/28/14.
//
// What changes would need to be made to StrBlobPtr to create a class
// that can be used with a const StrBlob?
// Define a class named ConstStrBlobPtr that can point to a const StrBlob.
//
// @See ex12_19.h
#ifndef CP5_ex12_22_h
#define CP5_ex12_22_h
#include <vector>
#include <string>
#include <initializer_list>
#include <memory>
#include <exception>
using std::vector; using std::string;
class ConstStrBlobPtr;
class StrBlob {
public:
using size_type = vector<string>::size_type;
friend class ConstStrBlobPtr;
ConstStrBlobPtr begin() const; // should add const
ConstStrBlobPtr end() const; // should add const
StrBlob():data(std::make_shared<vector<string>>()) { }
StrBlob(std::initializer_list<string> il):data(std::make_shared<vector<string>>(il)) { }
size_type size() const { return data->size(); }
bool empty() const { return data->empty(); }
void push_back(const string &t) { data->push_back(t); }
void pop_back() {
check(0, "pop_back on empty StrBlob");
data->pop_back();
}
std::string& front() {
check(0, "front on empty StrBlob");
return data->front();
}
std::string& back() {
check(0, "back on empty StrBlob");
return data->back();
}
const std::string& front() const {
check(0, "front on empty StrBlob");
return data->front();
}
const std::string& back() const {
check(0, "back on empty StrBlob");
return data->back();
}
private:
void check(size_type i, const string &msg) const {
if (i >= data->size()) throw std::out_of_range(msg);
}
private:
std::shared_ptr<vector<string>> data;
};
class ConstStrBlobPtr {
public:
ConstStrBlobPtr():curr(0) { }
ConstStrBlobPtr(const StrBlob &a, size_t sz = 0):wptr(a.data), curr(sz) { } // should add const
bool operator!=(ConstStrBlobPtr& p) { return p.curr != curr; }
const string& deref() const { // return value should add const
auto p = check(curr, "dereference past end");
return (*p)[curr];
}
ConstStrBlobPtr& incr() {
check(curr, "increment past end of StrBlobPtr");
++curr;
return *this;
}
private:
std::shared_ptr<vector<string>> check(size_t i, const string &msg) const {
auto ret = wptr.lock();
if (!ret) throw std::runtime_error("unbound StrBlobPtr");
if (i >= ret->size()) throw std::out_of_range(msg);
return ret;
}
std::weak_ptr<vector<string>> wptr;
size_t curr;
};
#endif
//
// ex12_22.cpp
// CP5
//
// Created by pezy on 1/2/15.
//
#include "ex12_22.h"
ConstStrBlobPtr StrBlob::begin() const // should add const
{
return ConstStrBlobPtr(*this);
}
ConstStrBlobPtr StrBlob::end() const // should add const
{
return ConstStrBlobPtr(*this, data->size());
}
1769



被折叠的 条评论
为什么被折叠?



