1. 程式人生 > >C++ Primer 第五版第三章習題答案

C++ Primer 第五版第三章習題答案

書籍版本:2019年9月第一版;王剛 楊巨峰譯;電子工業出版社

編譯器 : win10  && VS2015

3.1

對之前的練習用using來宣告,其餘與之前一致。

3.2


#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
	//string line;
	// 按行讀入
	//while(getline(cin,line))
	//{ 
	//	cout << line << endl;
	//}
	// 按詞讀入
	string word;
	while(cin>>word)
	{
		cout << word << endl;
	}

	system("pause");
    return 0;
}

3.3

string類忽略遇到第一個有效字元前的所有空格,遇到第一個有效字元之後所有空格都開始有效

getline跳過空格繼續向下讀取

3.4


#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string line1,line2;
	cout << "請輸入字串1:";
	cin >> line1;
	cout << "請輸入字串2:";
	cin >> line2;
	if (line1 == line2)
	{
		cout << "兩個字串相等" << endl;
	}
	else
	{
		cout << "兩個字串不相等" << endl;
	}
	if (line1.size() == line2.size())
	{
		cout << "兩個字串長度相等" << endl;
	}
	else
	{
		cout << "兩個字串長度不相等" << endl;
	}

	system("pause");
    return 0;
}

3.5


#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string line,strNoSpace,strSpace;
	while (cin>>line)
	{
		if (line == "break;")
		{
			break;
		}
		strNoSpace = strNoSpace + line;
		strSpace = strSpace + " ";
		strSpace = strSpace + line;
	}
	cout << "不帶空格:" << strNoSpace << endl;
	cout << "帶空格:" << strSpace << endl;

	system("pause");
    return 0;
}

3.6


#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string str("test");
	for (auto &s : str)
	{
		s = 'x';
	}
	cout << str;

	system("pause");
    return 0;
}

3.7

可以將型別換位char,具體操作就是將上面的auto換成char;結果無變化;

3.8


#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string str("test");
	int i = 0;
	for (i = 0;i<str.size();i++)
	{
		str[i] = 'x';
	}
	cout << "for:" << str << endl;
	while (i<str.size())
	{
		str[i] = 'x';
	}
	cout << "while:" << str << endl;

	system("pause");
    return 0;
}

3.9

不合法,s未賦初始值為空,沒有s[0];

3.10


#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string str("test,test,test");
	int i = 0;
	for (i = 0;i<str.size();i++)
	{
		if ( ispunct(str[i]) )
		{
			str[i] = ' ';
			for (int j = i; j < str.size(); j++)
			{
				str[j] = str[j + 1];
			}
		}
	}
	cout << str << endl;

	system("pause");
    return 0;
}

3.11

合法,型別為char

3.12

a 正確,建立了一個裡面是整形向量的向量;

b 錯誤,型別不同無法拷貝

c 正確,建立了一個string向量,裡面有10個值為null的string;

3.13

a 0個元素,空的

b 10個元素,都沒有值

c 10個值為42的int元素

d 1個值為10的元素

e 兩個元素,一個值為10,另一個為42

f 10個值為空的string元素,因為10不是string

g 10個值為hi的string元素;

3.14



#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;

int main()
{
	int num;
	vector<int> vec;
	while (cin>>num)
	{
		vec.push_back(num);
	}

	system("pause");
    return 0;
}

3.15


#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
	string str;
	vector<string> strVec;
	while (cin>>str)
	{
		strVec.push_back(str);
	}

	system("pause");
    return 0;
}

3.16

我之前的結果是對的,檢驗過了

3.17


#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
	string str;
	vector<string> strVec;
	while (cin>>str)
	{
		if (str == "break;")
		{
			break;
		}
		strVec.push_back(str);
	}
	for (int i = 0; i < strVec.size(); i++)
	{
		string strC = strVec.at(i);
			for (int j = 0; j < strC.size(); j++)
			{
				toupper(strC[j]);
			}
		cout << strC << endl;
	}

	system("pause");
    return 0;
}

3.18

不合法,改成

vector<int> ivec;

int num = 42;

ivec.push_back(num);

3.19

vector<int> numVec(10, 42);

vector<int> numVec{42,42,42,42,42,42,42,42,42,42};

vector<int> numVec;

int num = 42;

for(int i = 0;i<10;i++)

{

ivec.push_back(num);

}

3.20


#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
	string str;
	vector<int> strVec;
	int num[10] = { 1,2,3,4,5,6,7,8,9 };
	int m = 0;
	while (m<10)
	{
		strVec.push_back(num[m]);
		m++;
	}
	cout << "相連兩個:";
	for (int i = 0; i < strVec.size()-1; i++)
	{
		cout << strVec[i] + strVec[i + 1] << " ";
	}
	cout << endl;
	cout << "頭尾兩個:";
	for (int i = 0; i < strVec.size(); i++)
	{
		cout << strVec[i] + strVec[strVec.size()-1-i-1] << " ";
	}
	cout << endl;

	system("pause");
    return 0;
}

3.21


#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
	string str;
	vector<int> strVec;
	int num[10] = { 1,2,3,4,5,6,7,8,9,11 };
	int m = 0;
	while (m<10)
	{
		strVec.push_back(num[m]);
		m++;
	}
	vector<int>::iterator it;
	for (it = strVec.begin(); it != strVec.end(); it++)
	{
		cout << *it << endl;
	}

	system("pause");
    return 0;
}

3.22


#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
	string str("test,test");
	for (auto it = str.begin(); it != str.end(); it++)
	{
		*it = toupper(*it);
		cout << *it ;
	}

	system("pause");
    return 0;
}

3.23

=
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
	string str;
	vector<int> strVec = {1,2,3,4,5,6,7,8,9,10};
	vector<int>::iterator it;
	for (it = strVec.begin(); it != strVec.end(); it++)
	{
		*it = *it * 2;
		cout << *it << endl;
	}

	system("pause");
    return 0;
}

3.24


#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
	string str;
	vector<int> strVec = {1,2,3,4,5,6,7,8,9,10};
	vector<int>::iterator it;
	int n = 1;
	int sum;
	// 連續兩個相加
	for (it = strVec.begin(); it != strVec.end()-1; it++)
	{
		sum = *it + *(it + 1);
		cout << sum << endl;
	} 


	system("pause");
    return 0;
}

3.25


#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
	string str;
	vector<int> strVec;
	for (int i = 0; i < 10; i++)
	{
		int num;
		cin >> num;
		strVec.push_back(num);
	}
	
	vector<int> core(11, 0);
	vector<int>::iterator it;
	for (it = strVec.begin();it != strVec.end();it++)
	{
		int index = (*it) / 10;
		core[index]++;
	}
	for (it = core.begin(); it != core.end(); it++)
	{
		cout << *it << " ";
	}

	system("pause");
    return 0;
}

3.26

因為要求的mid是相對於從beg開始到end結束的中間值

3.27

a unsigned是非定長;

b 合法的

c 函式返回值是int型但是不是定值,所以是非定長的;

d fundamental有11個字元,字串陣列長度也設定為11缺少“\0”的位置

3.28

string sa[10] 與string sa2[10] 都為空,裡面無值

int ia[10];裡面值都為0;

int ia2[10];裡面值都為隨機值


#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

string sa[10];
int ia[10];
int main()
{
	string sa2[10];
	int ia2[10];
	for (int i = 0; i < 10; i++)
	{
		cout << sa[i] << endl;
		cout << ia[i] << endl;
		cout << sa2[i] << endl;
		cout << ia2[i] << endl;
		cout << "分割線" << endl;
	}
	

	system("pause");
    return 0;
}

3.29

陣列只能定長,無法自動增長,缺少靈活性,在用之前不能確定需要的空間大小時很不方便;

3.30

ix 只能小與array_size,等於時越界,因為長度為array_size的陣列下標範圍為0到array_size-1;

3.31


#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	int a[10];
	for (int i = 0; i < 10; i++)
	{
		a[i] = i;
	}
	for (int i = 0; i < 10; i++)
	{
		cout << a[i] << endl;
	}

	system("pause");
    return 0;
}

3.32


#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	int a[10];
	for (int i = 0; i < 10; i++)
	{
		a[i] = i;
	}
	for (int i = 0; i < 10; i++)
	{
		cout << a[i] << endl;
	}

	int a2[10];
	for (int i = 0; i < 10; i++)
	{
		a2[i] = a[i];
	}

	vector<int> vec;
	for (int i = 0; i < 10; i++)
	{
		vec.push_back(i);
	}

	system("pause");
    return 0;
}

3.33

不初始化裡面的值都是隨機值;

3.34

p1 += p2 - p1;  等價於  p1 = p2 - p1 + p1 = p2;

在p1p2不指向同一陣列時非法;

3.35



#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	int a[10];
	int* num = a;
	for (int i = 0; i < 10; i++)
	{
		a[i] = 0;
		cout << a[i] << endl;
	}

	system("pause");
    return 0;
}

3.36


#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
	int a[10],b[10];
	int* numa = a;
	int* numb = b;
	for (int i = 0; i < 10; i++)
	{
		a[i] = i;
		b[i] = 10 - i;
	}
	for (int i = 0; i < 10; i++)
	{
		if( *(numa+i) == *(numb+i) )
		{ 
		}
		else
		{
			cout << "不相等" << endl;
			break;
		}
	}

	vector<int> veca(10, 1);
	vector<int> vecb;
	for (int i = 0; i < 10; i++)
	{
		vecb.push_back(i);
	}

	for (int i = 0; i < 10; i++)
	{
		if(!veca[i] == vecb[i])
		{
			cout << "不相等" << endl;
			break;
		}
	}

	system("pause");
    return 0;
}

3.37


#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	const char ca[] = { 'h', 'e', 'l', 'l', 'o' };
	const char* cp = ca;
	while (*cp)
	{
		cout << *cp << endl;
		++cp;
	}

	system("pause");
    return 0;
}

結果如下:

3.38

指標就是地址,兩個地址相加之後是未知的存在。

3.39


#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

bool stringcomp(string str1, string str2)
{
	if (str1 == str2)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool cstringcomp(char s1[], char s2[])
{
	if (strcmp(s1, s2) == 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

int main()
{
	string str1 = "hello";
	string str2 = "hello!";
	char ca1[] = { 'h', 'e', 'l', 'l', 'o' , '\0'};
	char ca2[] = { 'h', 'e', 'l', 'l', 'o' , '!', '\0' };

	cout << stringcomp(str1, str2) << endl;
	cout << cstringcomp(ca1, ca2) << endl;

	system("pause");
    return 0;
}

3.40


#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
	char ca1[] = "hello";
	char ca2[] = "world";
	char str[11];
	strcpy_s(str, ca1);
	strcat_s(str, ca2);
	cout <<str << endl;

	system("pause");
    return 0;
}

因為我用的是VS2015,所以會自動提示strcmp與strcat是不安全的,原因是這些函式內部是不進行引數檢測的(包括越界類的),微軟擔心使用這些會造成記憶體異常,所以就改寫了同樣功能的函式,改寫了的函式進行了引數的檢測,使用這些新的函式會更安全和便捷,而改寫後安全的函式就是strcmp_s與strcat-s。

3.41



#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;

int main()
{
	int a[6];
	for (int i = 0; i < 6; i++)
	{
		a[i] = i;
	}
	vector<int> intVec(begin(a), end(a));
	for (int i = 0; i < intVec.size(); i++)
	{
		cout << intVec.at(i) << endl;
	}

	system("pause");
    return 0;
}

3.42



#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
	int a[6];
	for (int i = 0; i < 6; i++)
	{
		a[i] = i;
	}
	vector<int> intVec(begin(a), end(a));
	int a2[6];
	for (int i = 0; i < intVec.size(); i++)
	{
		a2[i] = intVec.at(i);
		cout << a2[i] << endl;
	}

	system("pause");
    return 0;
}

3.43



#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
		int ia[3][4] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };

		for (const int(&p)[4] : ia)
			for (int q : p) cout << q << " ";
		cout << endl;

		for (size_t i = 0; i != 3; ++i)
			for (size_t j = 0; j != 4; ++j) cout << ia[i][j] << " ";
		cout << endl;

		for (int(*p)[4] = ia; p != ia + 3; ++p)
			for (int* q = *p; q != *p + 4; ++q) cout << *q << " ";
		cout << endl;

	system("pause");
    return 0;
}

3.44



#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
	int ia[3][4] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
	using int_array = int[4];
	for (int_array& p : ia)
		for (int q : p) cout << q << " ";
	cout << endl;

	for (size_t i = 0; i != 3; ++i)
		for (size_t j = 0; j != 4; ++j) cout << ia[i][j] << " ";
	cout << endl;

	for (int_array* p = ia; p != ia + 3; ++p)
		for (int* q = *p; q != *p + 4; ++q) cout << *q << " ";
	cout << endl;

	system("pause");
    return 0;
}

3.45



#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
		int ia[3][4] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };

		// a range for to manage the iteration
		for (auto& p : ia)
			for (int q : p) cout << q << " ";
		cout << endl;

		// ordinary for loop using subscripts
		for (size_t i = 0; i != 3; ++i)
			for (size_t j = 0; j != 4; ++j) cout << ia[i][j] << " ";
		cout << endl;

		// using pointers.
		for (auto p = ia; p != ia + 3; ++p)
			for (int* q = *p; q != *p + 4; ++q) cout << *q << " ";
		cout << endl;

	system("pause");
    return 0;
}