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

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

#include "stdafx.h"
#ifndef STRBLOB_H
#define STRBLOB_H
#include <vector>
#include <string>
#include <memory>
#include <stdexcept>
#include <sstream>
#include <iostream>
#include<string>
#include<fstream>
using std::string;

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

class StrBlob {

    friend class StrBlobPtr;
public:
    typedef std::vector<std::string>::size_type size_type;

    // constructors
    StrBlob() : data(new std::vector<std::string>()) {

    }
    // 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<std::vector<std::string> > 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<std::string>(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<std::vector<std::string> >
        check(std::size_t, const std::string&) const;

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

};

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

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

}

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

    // is the vector still around?
    std::shared_ptr<std::vector<std::string> > 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<std::vector<std::string> >
        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"<<text;*/
    StrBlob str;
    while (std::getline(input, text))
    {
        std::istringstream stream(text);
        
        //bool firstword = true;
        while (stream >> s)
        {
            str.push_back(s);
        }

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


    return 0;
}
ifstream读取文件时要用“//”否则读取不了文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值