书籍版本:2019年9月第一版;王刚 杨巨峰译;电子工业出版社
编译器 : win10 && VS2015
这一篇从6.3节开始,也就是30题开始
6.30
报的是错误#1,没有正确的返回值
6.31
返回局部引用时无效,返回局部定义的常量引用无效。
6.32
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int& get(int* array, int index)
{
return array[index];
}
int main(int argc, char *argv[])
{
int ia[10];
for (int i = 0; i != 10; ++i)
{
get(ia, i) = i;
}
for (int i = 0; i < 10; i++)
{
cout << ia[i]<<endl;
}
system("pause");
}
函数合法,作用是取arry的index个成员的引用,并在main函数中调用该函数来对ia赋值;在后面加个输出就能看到效果了 ;
6.33
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void OutputVector(vector<int>::iterator begin, vector<int>::iterator end)
{
if (begin != end) {
cout << *begin << endl;
OutputVector(++begin, end);
}
}
int main(int argc, char *argv[])
{
vector<int> vec = { 0,1,2,3,4,5,6 };
OutputVector(vec.begin(), vec.end());
system("pause");
}
6.34
永远停不了
6.35
传入val--相当于factorial(val);val--;
val值不变,递归无法停止
6.36
string ( &func( string str[10] ) )[10]
6.37
// 类型别名
using strArray= string[10];
strArray& func(strArray& strArr);
// 尾置返回类型
auto func(string s[10]) -> string(&) [10]
{}
// decltype
string s[10];
decltype(s) & strArray(decltype(s)& )
{}
6.38
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int odd[] = { 1,3,5,7,9 };
int even[] = { 0,2,4,6,8 };
decltype(odd) &arrPtr(int i)
{
return (i % 2) ? odd : even;
}
int main(int argc, char *argv[])
{
int(&p)[5] = arrPtr(3);
for (auto i : p) {
cout << i << " " << endl;
}
system("pause");
}
6.39
a. 重复声明
b. 不合法,只有返回值不同无不同参数
c. 合法,参数、返回值类型都不同
6.40
b错误,如果要为参数加默认值,那么第一个添加默认值的参数的后面的所有参数都要加默认值。可以把需要加默认值的参数放在最后。
6.41
a. 非法,第一个参数没有默认值也没有传值
b. 合法
c. 合法,但是与初衷不符,因为第二个参数默认值为int ,却传入了一个char
6.42
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
string make_plural(size_t ctr, const string &word , const string &ending = "s")
{
return (ctr > 1) ? word + "e" + ending : word;
}
int main(int argc, char *argv[])
{
string str1 = "success";
string str2 = "failure";
cout << make_plural(2, str1) << endl;
cout << make_plural(2, str2) << endl;
system("pause");
}
6.43
a放在头文件中,因为是内联函数
b的声明放在头文件中,定义可以放在源文件中
6.44
inline bool isShorter(const string &s1, const string &s2)
{
return s1.size() < s2.size();
}
6.45
规模短小、流程直接、频繁调用的函数可以定义为内联函数
6.46
不能,因为isShorter函数中传入的参数不是字面值类型,str1.size() < str2.size()返回的也不是字面值类型。
6.47
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void OutputVector(vector<int>& ivec) {
#ifndef NDEBUG
cout << "vector's size is " << ivec.size() << endl;
#endif // NDEBUG
if (!ivec.empty())
{
auto tmp = ivec.back();
ivec.pop_back();
OutputVector(ivec);
cout << tmp << " ";
}
cout << endl;
}
int main(int argc, char *argv[])
{
vector<int> vec = { 0,1,2,3,4,5,6 };
OutputVector(vec.begin(), vec.end());
system("pause");
}
6.48
不合理,函数的意义是让用户进行输入,直到输入sought时停止。因此assert (cin)一直为真,这条语句也就没有意义。当while中的条件不成立时,说明没有输入或是输入为sought,这时assert也未必满足。
6.49
重载函数集合中的函数称为候选函数,候选函数具备两个特征:(1)与被调用的函数同名;(2)其声明在调用点可见。
从候选函数中选出能被这组实参调用的函数成为可行函数,可行函数也有两个特征:(1)其形参数量与本次调用提供的实参数量相等;(2)每个实参的类型与对应的形参类型相同,或是能转换成形参的类型。
6.50
a. f (2.56, 42) // 非法,因为实参类型是double, int,没有可匹配的函数。如果不是重载函数,只有一个声明f(double, double),则该语句合法。只有在重载时时非法的,要严格执行参数类型匹配。
b. f (42) // 调用 f (int)
c. f (42, 0) // 调用 f (int, int)
d. f (2.56, 3.14) // 调用 f (double, double = 3.14)
6.51
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void f()
{
cout << "没有参数" << endl;
}
void f(int a)
{
cout << "一个Int型参数:" << a << endl;
}
void f(int a, int b)
{
cout << "两个Int型参数:" << a << " 和 "<< b << endl;
}
void f(double a, double b = 3.14)
{
cout << "两个double型参数:" << a << " 和 " << b << endl;
}
int main(int argc, char *argv[])
{
//f(2.56, 42);
f(42);
f(42, 0);
f(2.56, 3.14);
system("pause");
}
6.52
a. 类型提升
b. 算数类型转换
6.53
a. 无影响
b. 无影响
c. 两个函数参数一样,系统会不知道掉用哪个
6.54
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int func(int a, int b)
{
return 0;
}
int main(int argc, char *argv[])
{
typedef decltype(func) * func2;
vector<func2> vec;
system("pause");
}
6.55
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int funcAdd(int a, int b)
{
return a + b;
}
int funcSub(int a, int b)
{
return a - b;
}
int funcMul(int a, int b)
{
return a * b;
}
int funcDiv(int a, int b)
{
return a / b;
}
int main(int argc, char *argv[])
{
typedef int(*p) (int, int);
vector<p> vec{ funcAdd, funcSub,funcMul, funcDiv };
system("pause");
}
6.56
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int funcAdd(int a, int b)
{
return a + b;
}
int funcSub(int a, int b)
{
return a - b;
}
int funcMul(int a, int b)
{
return a * b;
}
int funcDiv(int a, int b)
{
return a / b;
}
int main(int argc, char *argv[])
{
typedef int(*p) (int, int);
vector<p> vec{ funcAdd, funcSub,funcMul, funcDiv };
for (vector<p>::iterator iter = vec.begin(); iter != vec.end(); iter++)
{
cout << (*iter)(10, 2) << endl;
}
system("pause");
}
data:2018/11/28 21:02:37

被折叠的 条评论
为什么被折叠?



