C++ Primer 第5版--练习12.2
2017-05-19 20:58
281 查看
练习 12.2:编写你自己的StrBlob类,包含const版本的front和back。
#include <iostream>
#include <vector>
#include <memory>
#include <stdexcept>
using namespace std;
class StrBlob
{
public:
typedef vector<string>::size_type size_type;
StrBlob();
StrBlob(initializer_list<string> i1);
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& 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> i1) :
data(make_shared<vector<string>>(i1)) {}
void StrBlob::check(size_type i, const string &msg) const
{
if (i >= data->size())
throw out_of_range(msg);
}
string& StrBlob::front() const
{
check(0, "front on empty StrBlob");
return data->front();
}
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();
}
int main()
{
StrBlob b1;
StrBlob b2 = {"a", "an", "the"};
b1 = b2;
b2.push_back("about");
cout << "Elements number of b1: " << b1.size() << endl
<< "Elements number of b2: " << b2.size();
return 0;
}
#include <iostream>
#include <vector>
#include <memory>
#include <stdexcept>
using namespace std;
class StrBlob
{
public:
typedef vector<string>::size_type size_type;
StrBlob();
StrBlob(initializer_list<string> i1);
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& 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> i1) :
data(make_shared<vector<string>>(i1)) {}
void StrBlob::check(size_type i, const string &msg) const
{
if (i >= data->size())
throw out_of_range(msg);
}
string& StrBlob::front() const
{
check(0, "front on empty StrBlob");
return data->front();
}
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();
}
int main()
{
StrBlob b1;
StrBlob b2 = {"a", "an", "the"};
b1 = b2;
b2.push_back("about");
cout << "Elements number of b1: " << b1.size() << endl
<< "Elements number of b2: " << b2.size();
return 0;
}
相关文章推荐
- C++ Primer 第5版--练习12.6
- C++ Primer 第5版--练习8.5
- C++ Primer 第5版--练习10.11
- C++ Primer 第5版--练习12.7
- C++ Primer 第5版--练习6.56
- C++ Primer 第5版--练习9.14
- C++ Primer 第5版--练习10.33
- C++ Primer 第5版--练习9.31
- C++ Primer课后练习12.2
- C++ Primer 第5版--练习10.14
- C++ Primer 第5版--练习10.21
- C++ Primer 第5版--练习10.35
- C++ Primer 第5版--练习11.7
- C++ Primer 第5版--练习9.45 & 9.46
- C++ Primer 第5版--练习10.15
- C++ Primer 第5版--练习10.36.
- C++ Primer 第5版--练习11.20
- C++ Primer 第5版--练习10.13
- C++ primer 第五版 中文版 练习 12.2 个人code
- C++ Primer 第5版--练习10.18