C++ Primer 第五版第三章习题答案

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LL596214569/article/details/82724990

书籍版本: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;
}

猜你喜欢

转载自blog.csdn.net/LL596214569/article/details/82724990