C++Prime 第14章后23题
练习14.31
智能指针或内置类型,可以自己管理自己拥有的资源,于是使用编译器合成的拷贝控制函数就满足要求了.
练习14.32
class X
{
public:
string
* operator->() { return ptr
->operator->(); };
private:
StrBlobPtr
* ptr
;
};
练习14.33
重载的函数调用运算符最少接受0个对象,最多接受无限个对象.
练习14.34
#pragma once
#include <iostream>
using namespace std
;
class Test
{
public:
int operator()(int n1
, int n2
, int n3
) { if (n1
> 10) return n2
; else return n3
; };
private:
};
练习14.35
class ReadString
{
public:
string
operator()(istream
& i
= cin
) { getline(i
, mem
); return mem
; }
private:
string mem
;
istream
& is
= cin
;
};
练习14.36
int main()
{
string tmp
;
ReadString read
;
vector
<string
>data
;
while ((tmp
= read()) != "quit")
data
.push_back(tmp
);
for (auto x
: data
)
cout
<< x
<< endl
;
return 0;
}
练习14.37
int main()
{
vector
<int> data
{ 4,1,2,3,4,4,5,3,46,7 };
for (auto x
: data
)
cout
<< x
<< " ";
cout
<< endl
;
int n
;
while (cin
>> n
)
{
replace_if(data
.begin(), data
.end(), IsEqual(n
),9999);
for (auto x
: data
)
cout
<< x
<< " ";
cout
<< endl
;
}
return 0;
}
练习14.38
class Test
{
public:
Test() = default;
Test(int n
) :threshold(n
){ }
bool operator()(const string
& s
) { return s
.size() == threshold
; }
private:
int threshold
= 0;
};
int main()
{
vector
<int> data
{0,0,0,0,0,0,0,0,0,0,0};
int i
= 0;
while (++i
<= 10)
{
fstream
fin("word.txt");
if (!fin
)
throw runtime_error("cannot open "" word.txt");
string line
, word
;
while (getline(fin
, line
))
{
istringstream
sin(line
);
while (sin
>> word
)
{
Test
t(i
);
if (t(word
))
data
[i
]++;
}
}
}
Test t
;
for (int i
= 1; i
<= 10; ++i
)
cout
<< "长度为"<<i
<< "的单词有: "<< data
[i
]<<"个\n";
cout
<< endl
;
return 0;
}
练习14.39
从data[1]累加到data[9]
练习14.40
练习14.41
lambda可以内嵌在函数中,直接浏览函数定义,了解函数功能. 没有调用函数的开销.
需要实现的功能太为复杂时,使用类.
练习14.42
count_if(data
.begin(), data
.end(), bind(greater
<int>(),2));
find_if(svec
.begin(), svec
.end(), bind(not_equal_to
<string
>(),"pooh"));
transform(data
.begin(), data
.end(),data
.begin(), bind(multiplies
<int>(),2));
练习14.43
for_each(data
.begin(), data
.end(), ,bind(modulus
<int>(), _1
));
练习14.44
int add(int i
, int j
) { return i
+ j
; }
auto mod
= [](int i
, int j
) {return i
% j
; };
struct divide
{
int operator()(int denominator
, int divisor
) {
return denominator
/ divisor
;
}
};
int main()
{
map
<string
, function
<int(int, int)>> binops
;
binops
.insert({ "+",add
});
binops
.insert({ "%",mod
});
binops
.insert({ "/",divide() });
binops
.insert({ "-",[](int a
,int b
)->int {return a
- b
; } });
binops
.insert({ "*",multiplies
<int>() });
cout
<< binops
["+"](1, 3) << endl
;
cout
<< binops
["-"](1, 3) << endl
;
cout
<< binops
["*"](1, 3) << endl
;
cout
<< binops
["/"](1, 3) << endl
;
cout
<< binops
["%"](1, 3) << endl
;
return 0;
}
练习14.45
explicit operator string()const { return bookNo
; }
explicit operator double()const { return revenue
; }
练习14.46
应该声明类型装换运算符,便于转换. 应该声明成explicit.避免隐式转换酿成不可挽回的巨大悲剧.
练习14.47
operator const int() //将类转换成一个整型常量值,转换的过程中可以更改类对象 operator int() const //将类对象转换成一个普通int值,转换的过程中不可更改类对象
练习14.48
不需要转换
练习14.49
explicit operator bool()const { return m_name
!= " " && m_address
!= " "; }
练习14.50
ex1 会用到double() float()均有可能,二义性调用 ex2 会用到float(),精准匹配.
练习14.51
void cal(int)版本,标准转换优于类的自定义转换.
练习14.52
ld = si + ld; 内置版本的+,但是不确定使用哪一个.si->int,ld->float 或者double 二义性. ld = ld + si; 成员函数版本.
练习14.53
不合法的. 可以这样:3.14转换为int型的3,然后3->smalInt,调用友元版本的 operator+(const SmallInt&,const SmallInt&) 也可以这样:si转换成int,调用内置的int+double.二义性了 必须显示转换,即 explicit operator int()const{return valus;}