C++ Primer 第五版第六章習題答案(下)
書籍版本: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