首页 > 编程语言 >C++ primer StrBlobPtr和StrBlobPtr

C++ primer StrBlobPtr和StrBlobPtr

时间:2023-03-08 11:23:42浏览次数:28  
标签:std const string StrBlobPtr back C++ primer StrBlob

  1 #include <iostream>
  2 #include <stdio.h>
  3 #include <string>
  4 #include <memory>
  5 #include <algorithm>
  6 #include <initializer_list>
  7 #include <memory>
  8 #include <vector>
  9 #include <stdexcept>
 10 #include <fstream>
 11 #include <sstream>
 12 using namespace std;
 13 
 14 #ifndef STRBLOB_H_
 15 #define STRBLOB_H_
 16 
 17 class StrBlobPtr;
 18 
 22 class StrBlob
 23 {
 24 public:
 25     friend class StrBlobPtr;
 26     friend void inp(StrBlob& str, ifstream& input);
 27     typedef std::vector<std::string>::size_type size_type;
 28     StrBlob();
 29     StrBlob(std::initializer_list<std::string> il);
 30     size_type size() const { return data->size(); }
 31     bool empty() const { return data->empty(); }
 32     void push_back(const std::string& t) { data->push_back(t); }
 33     void pop_back();
 34     std::string& front();
 35     std::string& back();
 36     const std::string& front() const;
 37     const std::string& back() const;
 38     StrBlobPtr begin();
 39     StrBlobPtr end();
 40 private:
 41     std::shared_ptr<std::vector<std::string>> data;
 42     void check(size_type i, const std::string& msg) const;
 43 };
 44 
 45 class StrBlobPtr
 46 {
 47 public:
 48     friend void inp(StrBlob& str, ifstream& input);
 49     StrBlobPtr() : curr(0) {};
 50     StrBlobPtr(StrBlob& a, size_t sz = 0) : wptr(a.data), curr(sz) {}
 51     std::string& deref() const;
 52     StrBlobPtr& incr();
 53     friend bool eq(const StrBlobPtr& lhs, const StrBlobPtr& rhs);
 54 private:
 55     std::shared_ptr<std::vector<std::string>> check(std::size_t, const std::string&) const;
 56     std::weak_ptr<std::vector<std::string>> wptr;
 57     std::size_t curr;
 58 };
 59 
 60 std::shared_ptr<std::vector<std::string>> StrBlobPtr::check(std::size_t i, const std::string& msg) const
 61 {
 62     auto ret = wptr.lock();
 63     if (!ret)
 64         throw std::runtime_error("unbound StrBlobPtr");
 65     if (i >= ret->size())
 66         throw std::out_of_range(msg);
 67     return ret;
 68 }
 69 
 70 std::string& StrBlobPtr::deref() const
 71 {
 72     auto p = check(curr, "dereference past end");
 73     return (*p)[curr];
 74 }
 75 
 76 StrBlobPtr& StrBlobPtr::incr()
 77 {
 78     check(curr, "increment past end of StrBlobPtr");
 79     ++curr;
 80     return *this;
 81 }
 82 
 83 StrBlob::StrBlob() : data(std::make_shared<std::vector<std::string>>()) {}
 84 StrBlob::StrBlob(std::initializer_list<std::string> il) : data(std::make_shared<std::vector<std::string>>(il)) {}
 85 
 86 void StrBlob::check(size_type i, const std::string& msg) const
 87 {
 88     if (i >= data->size())
 89         throw std::out_of_range(msg);
 90 }
 91 
 92 std::string& StrBlob::front()
 93 {
 94     check(0, "front on empty StrBlob");
 95     return data->front();
 96 }
 97 
 98 std::string& StrBlob::back()
 99 {
100     check(0, "back on empty StrBlob");
101     return data->back();
102 }
103 
104 const std::string& StrBlob::front() const
105 {
106     check(0, "front on empty StrBlob");
107     return data->front();
108 }
109 
110 const std::string& StrBlob::back() const
111 {
112     check(0, "back on empty StrBlob");
113     return data->back();
114 }
115 
116 void StrBlob::pop_back()
117 {
118     check(0, "pop_back on empty StrBlob");
119     data->pop_back();
120 }
121 
122 StrBlobPtr StrBlob::begin() { return StrBlobPtr(*this); }
123 
124 StrBlobPtr StrBlob::end()
125 {
126     auto ret = StrBlobPtr(*this, data->size());
127     return ret;
128 }
129 
130 #endif
131 
132 bool eq(const StrBlobPtr& lhs, const StrBlobPtr& rhs) {
133     auto lp = lhs.wptr.lock(), rp = rhs.wptr.lock();
134     if (lp == rp) {
135         return (!lp || lhs.curr == rhs.curr);
136     }
137     else {
138         return false;
139     }
140 }
141 
142 void inp(StrBlob &str, ifstream &input)
143 {
144     //存入类中
145     string text;
146     while (getline(input, text))
147     {
148         istringstream stream(text);
149         string word;
150         while (stream >> word)
151         {
152             str.push_back(word);
153         }
154     }
155     //打印类中元素
156     for (StrBlobPtr cur = str.begin(); !eq(cur,str.end()); cur.incr())
157     {
158         string out = cur.deref();
159         cout << out << "";
160     }
161     
162 }

 

标签:std,const,string,StrBlobPtr,back,C++,primer,StrBlob
From: https://www.cnblogs.com/lihaoxiang/p/17191329.html

相关文章

  • C++11 thread_local关键字
    这是一篇科普文--关于thread_local关键字首先,C++11之前并没有对并发进行任何的支持,C++11首次提供了以下的支持:语言核心定义了一个内存模型,保证当更改"被两个不同线程使......
  • [Primer] 第 12 章 类和动态内存分配
    第12章类和动态内存分配12.1动态内存和类通常最好在程序运行时,而不是编译时决定内存的分配。不能在类声明中初始化静态变量,因为类声明仅仅提供了类的属性而没有分配......
  • 第一个C程序:如何在DevC++中编辑、编译和运行程序
    第一步:打开DevC++程序,选择文件——新建——源代码 第二步:保证输入法在英文状态下输入代码 第三步:保存文件,保存的时候选择.c文件格式 保存完之后*号消失 ......
  • C/C++课程设计题目及具体要求[2023-03-07]
    C/C++课程设计题目及具体要求[2023-03-07]三、课程设计题目及具体要求:(1)学生成绩管理程序要求:本程序用于教学单位(院/系)的学生成绩管理。要求程序能够实现学生信息......
  • [Effective] 1 让自己习惯 C++
    1让自己习惯C++条款01:视C++为一个语言联邦C++可以认为由4个次级语言组合而成:C是C++的语法基础;Object-orientedC++实现面向对象设计;TemplateC++实现泛型编......
  • 【C++】 结构化绑定“需要编译器标志“/std:c++17“
    问题  vs打开工程时报错:"结构化绑定"需要编译器标志"/std:c++17"  解决  ......
  • 蓝桥-13届-C++-B组-省赛-I题-李白打酒加强版
    最直接的做法,算是回溯吧:生成指定01数的序列挨个检查是否满足题意并计数能不能将生成和判断的过程统一呢?能不能记忆前面的序列呢瞄一眼题解,往动态规划的方向上靠#inc......
  • C++笔记-指针
    1.const指针和指向const的指针指向const的指针是在类型前加星号可以指向非const类型指针可以改变指向dereference不能改变值const指针是在类型后面加星号指针不可......
  • C++笔记-static本地变量
    static本地变量只能被本地看到,所以不同函数之间的static变量相同也没事,但是同一个函数调用多次会忽略后面的初始化。#include<iostream>voidmyStaticFunction(){......
  • c++笔架-编译,头文件,链接
    编译是按任意顺序进行的,并且每个文件是独立编译的,所以如果不用头文件的话,其他文件中定义的函数,在当前文件中是不可见的。也就是说没有定义。头文件只包含declaration,不包......