14.30
定义常量版本的* ->
//定义解引用运算符*和箭头运算符->
const std::string& operator*() const{
auto ret = check(curr,"越界");
return (*ret)[curr];
}
const std::string* operator->() const{
//将实际工作委托给*运算符
return & this->operator*();
}
14.31
因为StrBlobPtr的私有属性有一个weak_ptr,不需要考虑引用值的问题,一个curr下标基数,所以使用合成的就够了,不需要显示的定义
14.32
class StrBlobPtr;
class StrBlobPtr_pointer
{
public:
StrBlobPtr_pointer() = default;//默认构造函数
StrBlobPtr_pointer(StrBlobPtr* p) : pointer(p) { }//单参数构造函数,使用StrBlobPtr* 指针赋值
StrBlobPtr& operator *();//重载*
StrBlobPtr* operator->();//重载->
private:
StrBlobPtr* pointer = nullptr;//默认构造函数的StrBlobPtr类型的指针为空指针
};
14.33
等于该运算符使用的操作数个数
14.34
struct a{
operator()(bool a,int b,int c){
return a?b:c;
}
};
14.35
重新写()运算符,理解函数对象的概念
#include <iostream>
class PrintString{
public:
PrintString(std::istream &o = std::cin):is(o){}
std::string operator()(){
std::string s;
if(std::getline(is,s)){
return s;
}else{
return " ";
}
}
private:
std::istream &is;
};
int main()
{
PrintString p;
std::cout<<p()<< std::endl;;
return 0;
}
14.36
#include <iostream>
#include<vector>
using std::vector;
class PrintString{
public:
PrintString(std::istream &o = std::cin):is(o){}
std::string operator()(){
std::string s;
if(std::getline(is,s)){
return s;
}else{
return " ";
}
}
private:
std::istream &is;
};
int main()
{
vector<std::string> vec;
PrintString p;
int len=3;
while(len){
vec.push_back(p());
len--;
}
for(auto c:vec){
std::cout<<c<<" ";
}
return 0;
}
14.37
注意repace_if的用法
#include <iostream>
#include<vector>
#include<algorithm>
using std::vector;
class test{
public:
test(int s):val(s){}
bool operator()(int k){//调用函数
return val == k;
}
private:
int val;
};
int main()
{
std::vector<int> vec = { 77, 66, 55, 44, 33, 22, 11, 75 };
std::replace_if(vec.begin(), vec.end(), test(66), 5);//依次将每个元素构造成test的隐式对象,然后调用对象函数test(66),若是返回值为true,就用5替换
for(auto c:vec){
std::cout<<c<<std::endl;
}
return 0;
}
14.38
计算长度为1的数目,
#include <iostream>
#include<vector>
#include<algorithm>
#include<fstream>
using std::vector;
using std::string;
//14.38
class SizeComp{
public:
SizeComp(size_t n):sz(n){} //该形参对应捕获的变量
bool operator()(const string &s)const{
return s.size()== sz;
}
private:
size_t sz;
};
int main()
{
vector<std::string> vec;
string str;
std::ifstream ifs("C:\\study\\c++test\\endless.txt");
while(ifs>>str){
vec.push_back(str);
}
//计算长度为1
int len1=0;
for(auto c:vec){
if(c.size()==1){
len1++;
}
}
return 0;
}
14.39
计算1到9数目的和即可
14.40
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
class ShorterString
{
public:
bool operator()(string const& s1, string const& s2) const { return s1.size() < s2.size(); }
};
class BiggerEqual
{
size_t sz_;
public:
BiggerEqual(size_t sz) : sz_(sz) {}
bool operator()(string const& s) { return s.size() >= sz_; }
};
class Print
{
public:
void operator()(string const& s) { cout << s << " "; }
};
string make_plural(size_t ctr, string const& word, string const& ending)
{
return (ctr > 1) ? word + ending : word;
}
void elimDups(vector<string> &words)
{
sort(words.begin(), words.end());
auto end_unique = unique(words.begin(), words.end());
words.erase(end_unique, words.end());
}
void biggies(vector<string> &words, vector<string>::size_type sz)
{
elimDups(words);
stable_sort(words.begin(), words.end(), ShorterString());
auto wc = find_if(words.begin(), words.end(), BiggerEqual(sz));
auto count = words.end() - wc;
cout << count << " " << make_plural(count, "word", "s") << " of length " << sz << " or longer" << endl;
for_each(wc, words.end(), Print());
cout << endl;
}
int main()
{
vector<string> vec{ "fox", "jumps", "over", "quick", "red", "red", "slow", "the", "turtle" };
biggies(vec, 4);
}
14.41
使用方便,尤其是一个函数的功能单一且不常用的时候使用lambda表达式最为方便
网友评论