【C++Primer練習問題集】(第5版):練習12.2:constバージョンのfrontとbackを含む独自のStrBlobクラスを作成します.【スマートポインタの簡単な使用】


【C++Primer練習問題集】(第5版)練習12.2(P 298):constバージョンのfrontとbackを含む独自のStrBlobクラスを作成します.
#include
#include
#include
#include
#include
#include
#include
#pragma warning(disable:4996)
using std::string; using std::vector; using std::make_shared; using std::shared_ptr; using std::size_t;
using std::initializer_list; using std::out_of_range; using std::cin; using std::cout; using std::endl;
class StrBlob {
public://    
	StrBlob() { data = make_shared<vector<string>>(); }//         
	StrBlob(const initializer_list<string>& i) { data = make_shared<vector<string>>(i); }//    initializer_list        
	size_t size() const { return data->size(); }//             。
	bool empty() const { return data->empty(); }//        
	string& front() const { return data->front(); }//           
	string& back() const { return data->back(); }//           
	void emplace_back(const string& s) { data->emplace_back(s); }//         (C++11 )
	void push_back(const string& s) { data->push_back(s); }//         
	void pop_back() { oor(0, "ERROR: Popping back on an empty StrBlob."); data->pop_back(); }//  (  )       
	void clear() { data->clear(); }//           
	bool operator==(const StrBlob& b) {//   ==   ,      ==       StrBlob    
		if (this->size() != b.size())return false;
		for (size_t i = 0; i < this->size(); ++i)if ((*this->data)[i] != (*b.data)[i])return false;
		return true;
	}
	string& operator[](const size_t& n) { oor(n, "ERROR: Subscript out of range."); return (*data)[n]; }//   []   ,                     。      ,          。
	const string& operator[](const size_t& n) const { oor(n, "ERROR: Subscript out of range."); return (*data)[n]; }////   []   ,                     
private://    
	shared_ptr<vector<string>> data;//      (  )
	void oor(const size_t& s, const string& msg) const { if (s >= data->size())throw out_of_range(msg); }//        ,        
};
int main() {
	std::ios::sync_with_stdio(false), std::cin.tie(0);
	StrBlob b1, b2, b3;
	b1.emplace_back("sin"), b1.emplace_back("cos"), b1.emplace_back("tan");
	cout << b1[0] << ' ' << b1[1] << ' ' << b1[2] << '
'
; b1[0] = "SIN"; cout << b1[0] << '
'
; b1[0] = "sin"; b2 = b1; cout << b2[0] << ' ' << b2[1] << ' ' << b2[2] << '
'
; b2.emplace_back("cot"), b2.emplace_back("sec"), b2.emplace_back("csc"); cout << b1[0] << ' ' << b1[1] << ' ' << b1[2] << ' ' << b1[3] << ' ' << b1[4] << ' ' << b1[5] << endl; system("pause"); return 0; }