练习14.26
为你的String类定义下标运算符
char& operator[](size_t d) { return elements[d]; } const char& operator[](size_t d)const { return elements[d]; }
练习14.27
为你的StrBlobPtr添加递增递减运算符
StrBlobPtr& operator++() { check(curr,"beyond"); ++curr; return *this; } StrBlobPtr& operator--() { --curr; check(curr,"too small"); return *this; } StrBlobPtr operator++(int) { StrBlobPtr ret=*this; check(curr,"beyond"); ++curr; return ret; } StrBlobPtr operator--(int) { StrBlobPtr ret=*this; --curr; check(curr,"too small"); return ret; }
练习14.28
为你的StrBlobPtr添加加法和减法运算符
StrBlobPtr operator+(StrBlobPtr& a,StrBlobPtr& b) { StrBlobPtr c=a; c.curr=a.curr+b.curr; c.check(c.curr,"beyond"); return c; } StrBlobPtr operator-(StrBlobPtr& a,StrBlobPtr& b) { StrBlobPtr c=a; c.curr=a.curr-b.curr; c.check(c.curr,"too small"); return c; }
练习14.29
为什么不定义const版本的的递增和递减运算符
::StrBlobPtr的递增递减要修改curr
练习14.30
为你的StrBlobPtr添加解引用运算符和箭头运算符
std::string& operator*()const { auto p=check(curr,"dereference past end"); return (*p)[curr]; } std::string* operator->()const { return & this->operator*(); }
练习14.31
我们的StrBlobPtr没有定义拷贝构造函数,赋值运算符及析构函数为什么
::因为数据是weak_ptr,无需管理内存
练习14.32
定义一个类令其含有StrBlobPtr对象的指针,为这个类重载箭头运算符
class PTR{ public: StrBlobPtr& operator*()const { return (*this).ptr; } StrBlobPtr* operator->()const { return & this->operator*(); } private: StrBlobPtr* ptr; }
练习14.33
一个重载的函数调用运算符应该接受几个运算对象
::N+1
练习14.34
定义一个函数对象类,令其执行if-then-else的操作:该类的调用运算符接受三个形参,首先检查第一个,成功的话返回第二个,不成功返回第三个的值
struct ite{ int operator()(int a,int b,int c) { if(a==0) return b; return c; } };
练习14.35
编写一个类似PrintString的类,令其从输入流读取输入,返回所读取的string,如果失败,返回空string
using namespace std; struct ins{ ins(istream&i):in(i){ } string operator()() { string a; in>>a; return a; } istream& in; };
练习14.36
使用前一个类读取输入,并用vector保存
int main() { vector<string>ha; ins yyy(cin); for(int i=1;i!=10;++i) a.push_back(yyy()); for(auto d:a) cout<<d<<endl; }
练习14.37
编写一个类令其检查两个值是否相等,编写程序,令其替换序列等于某个给定值的所有实例
struct equa{ equa(const int &a):val(a){ } bool operator()(int &b) { return val==b; } int val; }; #include <vector> int main() { vector<int> vi{ 0,1,2,3,4,3,4,5 }; equa ci(2); replace_if(vi.begin(), vi.end(), ci,10); }
练习14.38
编写一个类令其检查某个给定的string对象的长度是否与一个阈值相等,统计并报告在输入的文件中长度为1的单词有多少个、长度为2的单词有多少个
struct thanl{ thanl(const size_t a):val(a){ } bool operator()(string &b) { return val==b.size(); } size_t val; }; #include <vector> #include <fstream> int main() { ifstream fin("number.txt"); vector<int> vi{ 0,1,2,3,4,3,4,5 }; thanl ci(2); string word; int count =0; while(fin>>word) { if(ci(word)) ++count; } cout<<count<<endl; }
练习14.39
修改上一题程序令其报告长度在1至9之间的有多少,10以上的有多少
struct thanl{ thanl(const size_t a):val(a){ } bool operator()(string &b) { return b.size()>1&&b.size()<9; } size_t val; }; #include <vector> #include <fstream> int main() { ifstream fin("number.txt"); vector<int> vi{ 0,1,2,3,4,3,4,5 }; thanl ci(2); string word; int count =0; while(fin>>word) { if(ci(word)) ++count; } cout<<count<<endl; }
struct thanl{ thanl(const size_t a):val(a){ } bool operator()(string &b) { return b.size()>10; } size_t val; };
练习14.41
你认为C++11新标准为什么要增加lambda?
::临时使用更方便吧
练习14.42
使用标准库函数对象及适配器定义一条表达式
(a)统计大于1024的值有多少
count_if(iv.begin(), iv.end(), bind(std::greater<int>(),_1, 1024))
(b)找到第一个不等于pooh的字符串
find_if(svec.cbegin(), svec.cend(), bind(std::not_equal_to<string>(), _1, "pooh"));
(C)将所有的值乘以2
transform(iv.begin(), iv.end(), iv.begin(), bind(std::multiplies<int>(), _1, 2));
练习14.43
使用标准库对象判断一个给定的int值是否能被int容器中的所有元素整除
any_of(iv.begin(), iv.end(), bind(std::modulus<int>(), 1024, _1));
标签:return,int,练习,StrBlobPtr,C++,operator,curr,14.26,primer From: https://www.cnblogs.com/yddl/p/16596747.html