6.16
bool is_empty(string& s) { return s.empty(); }
对于const的string 不适应,并且在不改变本体是时候,最后使用const来限制
6.17
#include <iostream>
#include <string>
using std::cout; using std::endl; using std::string;
bool any_capital(string const& str)
{
for (auto ch : str)
if (isupper(ch)) return true;
return false;
}
void to_lowercase(string& str)
{
for (auto& ch : str) ch = tolower(ch);
}
int main()
{
string hello("Hello World!");
cout << any_capital(hello) << endl;
to_lowercase(hello);
cout << hello << endl;
return 0;
}
6.18
bool compare(matrix &m1, matrix &m2){ /.../ }
//比较大小
vector<int>::iterator change_val(int,vector<int>::iterator) { /.../ }
//改变迭代器的位置,或者查找一个数什么的
6.19
double calc(double);
int count (const string &,char);
int sum (vector<int>::iterator,vector<int>::iterator,int);
vector<int> vec(10);
(a)calc(23.4,55.1); //不合法,只需要一个参数
(b)count("abcda",'a'); //合法
(c)calc(66); //合法
(d)sum(vector.begin(),vec.end(),3.8);//合法
6.20
值可能会被改变,而这不是你想要的
对于const的数据,这个函数可能不能用了
能用const的时候,一定要用const