65,186
社区成员




#ifndef STRBLOB
#define STRBLOB
#include <iostream>
#include <memory>
#include <vector>
#include <string>
#include <fstream>
using std::vector;
using std::string;
class StrBlobPtr;
class StrBlob {
friend class StrBlobPtr;
public:
typedef vector<string>::size_type size_type;
StrBlob();
StrBlob(std::initializer_list<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();
//元素访问
string& front();
string& front() const;
string& back();
string& back() const;
//返回指向首元素和尾后元素的StrBlobPtr
StrBlobPtr begin();
StrBlobPtr end();
private:
std::shared_ptr<vector<string>> data;
void check(size_type i, const string& msg) const;
};
StrBlob::StrBlob() {};
StrBlob::StrBlob(std::initializer_list<string> il) :
data(std::make_shared<vector<string>>(il)) {};
void StrBlob::pop_back()
{
check(0, "pop_back on empty vector");
data->pop_back();
}
string& StrBlob::front()
{
check(0, "front on empty vector");
return data->front();
}
string& StrBlob::front() const
{
check(0, "front on empty vector");
return data->front();
}
string& StrBlob::back()
{
check(0, "back on empty vector");
return data->back();
}
string& StrBlob::back() const
{
check(0, "back on empty vector");
return data->back();
}
void StrBlob::check(size_type i, const string& msg) const
{
if (i > data->size())
throw std::out_of_range(msg);
}
class StrBlobPtr {
public:
StrBlobPtr() :curr(0) {}
StrBlobPtr(StrBlob &a, size_t sz = 0) :
wptr(a.data), curr(sz) {}
std::string& deref()const;
StrBlobPtr& incr(); //前缀递增
private:
//若检查成功,check返回一个指向vector的shared_ptr
std::shared_ptr<vector<string>>
check(std::size_t, const std::string&)const;
//保存一个weak_ptr,意味着底层vector可能会被销毁
std::weak_ptr <vector<string>>wptr;
std::size_t curr; //在数组中的位置
};
std::shared_ptr<vector<string>>
StrBlobPtr::check(std::size_t i, const string &msg)const
{
auto ret = wptr.lock(); //wptr所指的vector还存在吗
if (!ret)
throw std::runtime_error("unbound StrBlobPtr");
if (i >= ret->size())
throw std::out_of_range(msg);
return ret; //否则返还指向vector的shared_ptr
}
std::string& StrBlobPtr::deref()const
{
auto p = check(curr, "dereference past end");
return (*p)[curr]; //返回vector的curr坐标的字符串
}
StrBlobPtr& StrBlobPtr::incr()
{
//如果curr已经指向容器的尾后位置,就不能递增它
check(curr, "increment past end of StrBlobPtr");
++curr; //推进当前位置
return *this;
}
StrBlobPtr StrBlob::begin() { return StrBlobPtr(*this); }
StrBlobPtr StrBlob::end()
{
auto ret = StrBlobPtr(*this, data->size());
return ret;
}
#endif
int main()
{
std::ifstream in("str.txt");
if (!in) {
std::cout << "open error!" << std::endl;
return -1;
}
StrBlob blob;
for (std::string str; std::getline(in, str);) blob.push_back(str);
return 0;
}