c++primer第五版第十二章12.20习题用一个StrBlobPtr打印出StrBlob中的元素

// ConsoleApplication2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#ifndef STRBLOB_H
#define STRBLOB_H
#include
#include
#include
#include
#include
#include
#include
#include
using std::string;

// forward declaration needed for friend declaration in StrBlob
class StrBlobPtr;

class StrBlob {

    friend class StrBlobPtr;
public:
    typedef std::vector::size_type size_type;

    // constructors
    StrBlob() : data(new std::vector()) {

    }
    // pre C++11 no initializer_list, we'll define a constructor
    // that takes pointers to an array instead
    StrBlob(const std::string*, const std::string*);

    // size operations
    size_type size() const {
        return data->size();
    }
    bool empty() const {
        return data->empty();
    }

    // add and remove elements
    void push_back(const std::string &t) {
        data->push_back(t);
    }
    void pop_back();

    // element access
    std::string& front();
    std::string& back();

    // interface to StrBlobPtr
    StrBlobPtr begin();  // can't be defined until StrBlobPtr is
    StrBlobPtr end();
private:
    std::shared_ptr > data;
    // throws msg if data[i] isn't valid
    void check(size_type i, const std::string &msg) const;

};

// constructor
inline
StrBlob::StrBlob(const std::string *beg, const std::string *end) :
    data(new std::vector(beg, end)) {

}

// StrBlobPtr throws an exception on attempts to access a nonexistent element
class StrBlobPtr {

    friend bool eq(const StrBlobPtr&, const StrBlobPtr&);
public:
    StrBlobPtr() : curr(0) {

    }
    StrBlobPtr(StrBlob &a, size_t sz = 0) : wptr(a.data), curr(sz) {

    }

    std::string& deref() const;
    StrBlobPtr& incr();       // prefix version
    StrBlobPtr& decr();       // prefix version
private:
    // check returns a shared_ptr to the vector if the check succeeds
    std::shared_ptr >
        check(std::size_t, const std::string&) const;

    // store a weak_ptr, which means the underlying vector might be destroyed
    std::weak_ptr > wptr;
    std::size_t curr;      // current position within the array

};

inline
std::string& StrBlobPtr::deref() const
{

    std::shared_ptr >
        p = check(curr, "dereference past end");
    return (*p)[curr];  // (*p) is the vector to which this object points

}

inline
std::shared_ptr >
StrBlobPtr::check(std::size_t i, const std::string &msg) const
{

    // is the vector still around?
    std::shared_ptr > ret = wptr.lock();

    if (!ret)
        throw std::runtime_error("unbound StrBlobPtr");

    if (i >= ret->size())
        throw std::out_of_range(msg);

    return ret; // otherwise, return a shared_ptr to the vector

}

// prefix: return a reference to the incremented object
inline
StrBlobPtr& StrBlobPtr::incr()
{

    // if curr already points past the end of the container, can't increment it
    check(curr, "increment past end of StrBlobPtr");
    ++curr;       // advance the current state
    return *this;

}

inline
StrBlobPtr& StrBlobPtr::decr()
{

    // if curr is zero, decrementing it will yield an invalid subscript
    --curr;       // move the current state back one element
check(-1, "decrement past begin of StrBlobPtr");
return *this;

}

// begin and end members for StrBlob
inline
StrBlobPtr
StrBlob::begin()
{

    return StrBlobPtr(*this);

}

inline
StrBlobPtr
StrBlob::end()
{

    StrBlobPtr ret = StrBlobPtr(*this, data->size());
    return ret;

}

// named equality operators for StrBlobPtr
inline
bool eq(const StrBlobPtr &lhs, const StrBlobPtr &rhs)
{

    std::shared_ptr >
        l = lhs.wptr.lock(), r = rhs.wptr.lock();
    // if the underlying vector is the same
    if (l == r)
        // then they're equal if they're both null or
        // if they point to the same element
        return (!r || lhs.curr == rhs.curr);
    else
        return false; // if they point to difference vectors, they're not equal

}

inline
bool neq(const StrBlobPtr &lhs, const StrBlobPtr &rhs)
{

    return !eq(lhs, rhs);

}
#endif

int main()
{
    string a, b;
    string text,s;
    std::ifstream input("E://a.txt");
    /*input >> a >> b;
    std::cout << a;*/
    //std::cout << input;
    /*std::getline(input, text);
    std::cout <<"dd"<     StrBlob str;
    while (std::getline(input, text))
    {
        std::istringstream stream(text);
        
        //bool firstword = true;
        while (stream >> s)
        {
            str.push_back(s);
        }

        std::cout << "s"<             
    }
    StrBlobPtr sp(str);
    while (1)
    {
        std::cout << sp.deref() <         sp.incr();
    }
            
        


    return 0;
}
ifstream读取文件时要用“//”否则读取不了文件

你可能感兴趣的:(c++primer第五版第十二章12.20习题用一个StrBlobPtr打印出StrBlob中的元素)