//12.2
#pragma once
#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
{
if (i >= data->size())
throw out_of_range(msg);
}
};
StrBlob::StrBlob():data(make_shared<vector<string>>()) { }
StrBlob::StrBlob(initializer_list<string> il): data(make_shared<vector<string>> (il)) { }
string& StrBlob::front()
{
check(0, "front on empty StrBlod");
return data->front();
}
const string& StrBlob::front()const
{
check(0, "front on empty StrBlod");
return data->front();
}
string& StrBlob::back()
{
check(0, "front on empty StrBlod");
return data->back();
}
const string& StrBlob::back() const
{
check(0, "front on empty StrBlod");
return data->back();
}
void StrBlob::pop_back()
{
check(0, "front on empty StrBlod");
return data->pop_back();
}
#include<iostream>
#include"StrBlob.h"
using namespace std;
int main()
{
StrBlob b1;
{
StrBlob b2 = {"a","an","the"};
b1 = b2;
b2.push_back("about");
cout << b2.size();
}
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 &tem : *pv)
cout << tem << " ";
cout << endl;
}
int main()
{
vector<int> *pv = new_vector();
read_ints(pv);
print_ints(pv);
delete pv;
pv = nullptr;
return 0;
}
//12.7
#include<iostream>
#include<vector>
#include<memory>
using namespace std;
//创建新的内存
shared_ptr<vector<int>> new_vector(void)
{
return make_shared<vector<int>> ();
}
//读入数据
void read_ints(shared_ptr<vector<int>> pv)
{
int v;
while (cin >> v)
pv->push_back(v);
}
//输出数据
void print_ints(shared_ptr<vector<int>> pv)
{
for (const auto &tem : *pv)
cout << tem << " ";
cout << endl;
}
int main()
{
auto pv = new_vector();
read_ints(pv);
print_ints(pv);
return 0;
}
//12.14
#include<iostream>
#include<memory>
using namespace std;
struct destination {};
struct connection {};
connection connect(destination *pd)
{
cout << "打开连接" << endl;
return connection();
}
void disnnection(connection c)
{
cout << "断开连接" << endl;
}
void f(destination &d)
{
cout << "直接管理connect" << endl;
connection c = connect(&d);
cout << endl;
}
void end_connection(connection *p) { disnnection(*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()
{
destination d;
f(d);
f1(d);
return 0;
}
//12.15
#include<iostream>
#include<memory>
using namespace std;
struct destination {};
struct connection {};
connection connect(destination *pd)
{
cout << "打开连接" << endl;
return connection();
}
void disnnection(connection c)
{
cout << "断开连接" << endl;
}
void f(destination &d)
{
cout << "直接管理connect" << endl;
connection c = connect(&d);
cout << endl;
}
void f1(destination &d)
{
cout << "用shared_ptr管理connect" << endl;
connection c = connect(&d);
shared_ptr<connection> p(&c, [](connection *p) { disnnection(*p); });
cout << endl;
}
int main()
{
destination d;
f(d);
f1(d);
return 0;
}
//12.19
#pragma once
#include<vector>
#include<string>
#include<initializer_list>
#include<memory>
#include<stdexcept>
using namespace std;
class StrBlob
{
friend class StrBlobPtr;
public:
StrBlobPtr begin();//调用的构造函数
StrBlobPtr end();
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
{
if (i >= data->size())
throw out_of_range(msg);
}
};
//核查指针类
class StrBlobPtr {
friend bool eq(const StrBlobPtr &lhs, const StrBlobPtr &rhs);
public:
StrBlobPtr() :curr(0) { }
StrBlobPtr(StrBlob &a, size_t sz = 0) :wptr(a.data), curr(sz) { }
string &deref() const;//解引用
StrBlobPtr &incr();//前缀递增
private:
shared_ptr<vector<string>> check(size_t, const string&) const;
weak_ptr<vector<string>> wptr;
size_t curr;
};
StrBlob::StrBlob():data(make_shared<vector<string>>()) { }
StrBlob::StrBlob(initializer_list<string> il): data(make_shared<vector<string>> (il)) { }
string& StrBlob::front()
{
check(0, "front on empty StrBlod");
return data->front();
}
const string& StrBlob::front()const
{
check(0, "front on empty StrBlod");
return data->front();
}
string& StrBlob::back()
{
check(0, "front on empty StrBlod");
return data->back();
}
const string& StrBlob::back() const
{
check(0, "front on empty StrBlod");
return data->back();
}
void StrBlob::pop_back()
{
check(0, "front on empty StrBlod");
return data->pop_back();
}
shared_ptr<vector<string>> StrBlobPtr::check(size_t i, const string &msg) const
{
auto ret = wptr.lock();
if (!ret)
throw runtime_error("unbound StrBlobPtr");
if (i >= ret->size())
throw out_of_range(msg);
return ret;
}
string &StrBlobPtr::deref() const
{
auto p = check(curr, "erro");
return (*p)[curr];
}
StrBlobPtr &StrBlobPtr::incr()
{
check(curr, "已到尾后");
++curr;
return *this;
}
bool eq(const StrBlobPtr &lhs, const StrBlobPtr &rhs)
{
auto l = lhs.wptr.lock(), r = rhs.wptr.lock();
if (l == r)
return (!r || lhs.curr == rhs.curr);
else
return false;
}
bool neq(const StrBlobPtr &lhs, const StrBlobPtr &rhs)
{
return !eq(lhs,rhs);
}
StrBlobPtr StrBlob::begin() { return StrBlobPtr(*this); }//调用的构造函数
StrBlobPtr StrBlob::end() {
auto ret = StrBlobPtr(*this, data->size());
return ret;
}
#include<iostream>
#include"StrBlob.h"
#include<memory>
using namespace std;
int main()
{
StrBlob b1 = { "a","bad","boy" };
for (auto it = b1.begin();neq(it, b1.end());it.incr())
cout << it.deref() << endl;
return 0;
}
//12.20
#include<iostream>
#include"StrBlob.h"
#include<fstream>
#include<memory>
using namespace std;
int main(int argc,char *argv[])
{
ifstream in(argv[1]);
if (!in)
cerr << "erro" << endl;
StrBlob b1;
string s;
while (getline(in, s))
b1.push_back(s);
for (auto it = b1.begin();neq(it, b1.end());it.incr())
cout << it.deref() << endl;
return 0;
}
//12.23
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
const char *c1 = "Hello";
const char *c2 = "World";
char *c = new char[strlen(c1)+strlen(c2)+1];
strcpy(c, c1);
strcat(c, c2);
cout << c << endl;
string s1 = "hello";
string s2 = "world";
strcpy(c, (s1+s2).c_str());
cout << c << endl;
delete[]c;
return 0;
}
//12.24
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char c;
char *r = new char[10];
int num = 0;
while (cin.get(c))
{
if (isspace(c))
break;
r[num++] = c;
if (num == 10)
{
cout << "The container has been filled" << endl;
break;
}
}
r[num] = 0;
cout << r << endl;
delete[]r;
return 0;
}
//12.26
#include<iostream>
#include<memory>
#include<string>
using namespace std;
int main()
{
allocator<string> alloc;
auto const p = alloc.allocate(5);
string s;
string *q = p;
while (cin >> s && q != p + 5)
alloc.construct(q++, s);
const size_t sz = q - p;
for (auto i = 0;i < sz;i++)
cout << p[i] << " ";
cout << endl;
while (q != p)
alloc.destroy(--q);
alloc.deallocate(q,100);
return 0;
}