C++Primer
来源:互联网 发布:唇色太深怎么变浅知乎 编辑:程序博客网 时间:2024/04/27 22:43
12.2
#ifndef MY_STRBLOB_H#define MY_STRBLOB_H#include<vector>#include<string>#include<initializer_list>#include<memory>#include<stdexcept>using namespace std;class StrBlob{public: typedef vector<string>::size_type size_type; StrBlob(); StrBlob(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(); const string& front() const; string& back(); const string& back() const;private: shared_ptr<vector<string>> data; void check(size_type i, const string &msg)const;};StrBlob::StrBlob() :data(make_shared<vector<string>>()) {}StrBlob::StrBlob(initializer_list<string>il): data(make_shared<vector<string>>(il)){}void StrBlob::check(size_type i, const string &msg)const{ if (i >= data->size()) throw out_of_range(msg);}string& StrBlob::front(){ check(0, "front on empty StrBlob"); return data->front();}const string& StrBlob::front() const{ check(0, "front on empty StrBlob"); return data->front();}string& StrBlob::back(){ check(0, "back on empty StrBlob"); return data->back();}const string& StrBlob::back() const{ check(0, "back on empty StrBlob"); return data->back();}void StrBlob::pop_back(){ check(0, "pop_back on empty StrBlob"); data->pop_back();}#endif
测试
#include<iostream>using namespace std;#include "my_StrBlob.h"int main(int argc,char **argv){ StrBlob b1; { StrBlob b2 = { "a","an","the" }; b1 = b2; b2.push_back("about"); cout << b2.size() << endl; } cout << b1.size() << endl; cout << b1.front() << " " << b1.back() << endl; const StrBlob b3 = b1; cout <<b3.front() << " " << b3.back() << endl; return 0;}
12.6
#include<iostream>#include<vector>using namespace std;vector<int> *new_vector(void){ return new (nothrow)vector<int>;}void read_ints(vector<int>*pv){ int v; while (cin >> v) { pv->push_back(v); }}void print_ints(vector<int> *pv){ for (const auto &v : *pv) { cout << v << " "; } cout << endl;}int main(){ vector<int> *pv = new_vector(); if (!pv) { cout << "内存不足!" << endl; return -1; } read_ints(pv); print_ints(pv); delete pv; pv = nullptr; return 0;}
12.14
#include<iostream>#include<vector>#include<memory>using namespace std;struct destination{ };struct connection{ };connection connect(destination *pd){ cout << "打开连接" << endl; return connection();}void disconnect(connection c){ cout << "关闭连接" << endl;}void f(destination &d){ cout << "直接管理connect" << endl; connection c = connect(&d); cout << endl;}void end_connection(connection *p) { disconnect(*p); }void f1(destination &d){ cout << "用shared_ptr管理connect" << endl; connection c = connect(&d); shared_ptr<connection> p(&c, end_connection); cout << endl;}int main(int argc,char**argv){ destination d; f(d); f1(d); return 0;}
0 0
- C++primer
- 《C++primer》
- C++primer
- C++primer
- C++Primer
- 重读C++Primer-1
- C++Primer Dialy
- c++primer 13.7联合
- C++Primer 13.9类域
- c++primer学习小记
- C-PRIMER PLUS读书笔记
- C-PRIMER PLUS读书笔记
- C++Primer
- C++Primer学习小记
- c++primer问题思考
- 读c++primer
- C++Primer读书笔记(一)
- C++PRIMER学习笔记
- R语言图形界面的shiny软件包的方法
- 初步认识Retrofit
- 继承-代码的复用
- loadrunner 播放脚本后,日志显示中文乱码解决方法
- php CSV表格导入导出
- C++Primer
- BZOJ 1109: [POI2007]堆积木Klo LIS
- RecyclerView 的工具类,更方便的实现 Adapter,item 点击事件,更快的实现加载提示,分页加载。
- npm如何升级自身
- 用Aspose.Words 从Word文档中提取表格数据
- Android之ListView缓存分页加载
- 语音增强方法分类
- Weex之Android端的浅析(一)
- 超简单实现图片裁切功能