C++Primer第五版 ——— (ch3)课后习题参考答案

练习 3.1

使用恰当的 using 声明重做 1.4.1节(第 11 页)和 2.6.2节(第 67 页)的练习。

using namespace std ;(将标准库声明)-------前面已使用了,这里不再重复。

练习 3.2

编写一段程序从标准输入中一次读入一整行,然后修改该程序使其一次读入一个词。

#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 << " ";
	cout << endl;

	return 0;
}

在这里插入图片描述

练习 3.3

请说明 string 类的输入运算符和 getline 函数分别是如何处理空白字符的。

string 类
自动忽略开头空白,并从第一个真正的字符开始读起,直到遇见下一个空白为止,即读到除开头的第一个空白停止;
getline 函数
读入一整行,直到换行符为止,开头是换行符也读入换行符停止。

练习 3.4

编写一段程序读取两个字符串,比较其是否相等并输出结果。如果不相等,输出比较大的那个字符串。改写上述程序,比较输入的两个字符串是否等长,如果不等长,输出长度较大的那个字符串。

#include <iostream>
#include <string>
using namespace std;

int main()
{
	//字符串比较:
	string word0, word1;
	cin >> word0 >> word1;
	if (word0 == word1)
		cout << "word0 = word1 = " << word0 << endl;
	else
		cout << (word0 > word1 ? word0 : word1) << endl;
	//字符串长度比较:
	string word2, word3;
	cin >> word2 >> word3;
	if (word2.size() == word3.size())
		cout <<  word2 << endl;
	else
		cout << (word2.size() > word3.size() ? word2 : word3) << endl;

	return 0;
}

在这里插入图片描述

练习 3.5

编写一段程序从标准输入中读入多个字符串并将他们连接起来,输出连接成的大字符串。然后修改上述程序,用空格把输入的多个字符串分割开来。

#include <iostream>
#include <string>
using namespace std;

int main()
{
	//多个字符串将他们连接起来
	string word,s;
	while (cin >> word)
		s += word;
	cout << s << endl;
	//空格把输入的多个字符串分割开来
	//#1
	string word1, s1, line;
	while (cin >> word1)
		s1 += " " + word1;
	cout << s1 << endl;
	//#2
	getline(cin, line);
	cout << line << endl;
	
	return 0;
}

在这里插入图片描述

练习 3.6

编写一段程序,使用范围 for 语句将字符串内所有字符用 X 代替

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string s;
	cin >> s;
	for (auto& c : s)
		c = 'X';
	cout << s << endl;

	return 0;
}

在这里插入图片描述

练习 3.7

就上一题完成的程序而言,如果将循环控制的变量设置为char 将发生什么?先估计一下结果,然后实际编程进行验证。

在这里插入图片描述

练习3.8

分别用while循环和传统for循环重写第一题的程序,你觉得哪种形式更好呢?为什么?

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string s;
	getline(cin, s);
	//传统for循环
	for (int i=0 ; i < s.size(); ++i)
		s[i] = 'X';
	cout << s << endl;
	//while循环
	int j = 0;
	while (j < s.size())
	{
		s[j] = 'X';
		++j;
	}
	cout << s << endl;

	return 0;
}

练习 3.9

下面的程序有何作用?它合法吗?如果不合法?为什么?

string s;
cout << s[0] << endl;

打印出字符串 s 的第一个字符。
非法。空字符串不存在首字符。

练习 3.10

编写一段程序,读入一个包含标点符号的字符串,将标点符号去除后输出字符串剩余的部分。

#include <iostream>
#include <string>
using namespace std;

int main()
{
	//#1
	string s;
	int i = 0;
	getline(cin, s);
	for (auto& c : s)
	{
		if (ispunct(c))
			continue;
		cout << c;
	}
	//#2
	string str_in, str;
	getline(cin, str_in);
	for (auto c : str_in)
		if (!ispunct(c))
			str += c;
	cout << str << endl;

	return 0;
}

在这里插入图片描述

练习 3.11

下面的范围 for 语句合法吗?如果合法,c 的类型是什么?

const string s = "Keep out!";
for(auto &c : s){ /* ... */ }

合法,c 是对 const char 的引用。但是c值不可改变!!!
在这里插入图片描述

练习题3.12

下列vector对象的定义是否正确?说明原因

( a )vector<vector<int>> ivec;
( b )vector<string> svec = ivec; 
( c )vector<string>svec(10, "null"); 

a:正确,创建了一个元素为vector的vector对象

b:不正确,类型不一致

c:正确,十个“null”对象

练习题3.13

下列vector对象各包含多少个元素?值分别是多少?

( a )vector<int> v1; //不包含元素,初始状态为空。
( b )vector<int> v2(10); //包含10个元素,都初始化为0。
( c )vector<int> v3(10, 42);// 包含10个元素,都初始化为42。
( d )vector<int> v4{10}; //包含1个元素,值为10。
( e )vector<int> v5{10, 42};//包含2个元素,值分别是10和42。
( f )vector<string> v6{10}; //10个默认初始化的元素。
( g )vector<string> v7{10, "hi"};//10个值为hi的元素。

练习题3.14

编写一段程序,用cin读入一组整数并把它们存入vector对象。

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
	int i;
	vector<int>temp;
	while (cin >> i)
		temp.push_back(i);
	for (auto& j : temp)
		cout << j << " ";
	cout << endl;
		
	return 0;
}

在这里插入图片描述

练习题3.15

改写上题程序,读入字符串

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
	string word;
	vector<string>temp;
	while (cin >> word)
		temp.push_back(word);
	for (auto& j : temp)
		cout << j << " ";
	cout << endl;
		
	return 0;
}

练习题3.16

编写程序,将13题中的vector对象容量和值输出。检验之前的回答是否正确

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
	vector<int> v1;
	if (!v1.empty())
	{
		for (auto v : v1)
			cout << v << " ";
		cout << endl;
		cout << "size:" << v1.size() << endl;
	}
	else cout << "noll" << endl;
		
	vector<int> v2(10);
	if (!v2.empty())
	{
		for (auto v : v2)
			cout << v << " ";
		cout << endl;
		cout << "size:" << v2.size() << endl;
	}
	else cout << "noll" << endl;

	vector<int> v3(10, 42);
	if (!v3.empty())
	{
		for (auto v : v3)
			cout << v << " ";
		cout << endl;
		cout << "size:" << v3.size() << endl;
	}
	else cout << "noll" << endl;

	vector<int> v4{ 10 };
	if (!v4.empty())
	{
		for (auto v : v4)
			cout << v << " ";
		cout << endl;
		cout << "size:" << v4.size() << endl;
	}
	else cout << "noll" << endl;

	vector<int> v5{ 10, 42 }; 
	if (!v5.empty())
	{
		for (auto v : v5)
			cout << v << " ";
		cout << endl;
		cout << "size:" << v5.size() << endl;
	}
	else cout << "noll" << endl;

	vector<string> v6{ 10 }; 
	if (!v1.empty())
	{
		for (auto v : v1)
			cout << v << " ";
		cout << endl;
		cout << "size:" << v1.size() << endl;
	}
	else cout << "noll" << endl;

	vector<string> v7{ 10, "hi" }; 
	if (!v1.empty())
	{
		for (auto v : v1)
			cout << v << " ";
		cout << endl;
		cout << "size:" << v1.size() << endl;
	}
	else cout << "noll" << endl;
		
	return 0;
}

在这里插入图片描述

练习题3.18

下面的程序合法吗?该如何修改?

vector<int> ivec;
ivec[0] = 42;

不合法,ivec为空,不包含任何元素,不能通过下标去访问元素。
修改为:ivec.push_back(42);

练习题3.19

如果想定义一个含有10个元素的vector元素,所有元素值都为42,请列举三种不同的方式,哪种更好 ? 原因?

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
	vector<int>(10, 42);
	vector<int>{42,42,42,42,42,42,42,42,42,42};
	vector<int>i(10);
	for (auto& j : i)
	{
		j = 42;
		cout << j << " ";
	}
		
	return 0;
}

练习题3.20

读入一组整数并把它们存入一个vector对象,将每对相邻整数的和输出出来,改写程序,这次要求先输出第一个和最后一个之和,接着输出第二个和倒数第二个之和,依次类推。

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
	vector<int>t;
	int i;
	while (cin >> i)
		t.push_back(i);
	cout << "相邻整数的和:";
	for (int j = 0; j < t.size()-1; j++)
		cout << t[j] + t[j + 1] << " ";
	cout << endl;
	cout << "第一个和最后一个之和:";
	if (t.size() % 2 == 0)
	{
		for (int j = 0; j < t.size() / 2; j++)
			cout << t[j] + t[t.size() - 1 - j] << " ";
		cout << endl;
	}
	else
	{
		for (int j = 0; j < t.size() / 2; j++)
			cout << t[j] + t[t.size() - 1 - j] << " ";
		cout << t[t.size() / 2] << endl;
	}
	
	return 0;
}

在这里插入图片描述
在这里插入图片描述

练习题3.21

请使用迭代器重做3.16题。

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
	vector<int> v1;
	if (v1.begin()!=v1.end())
	{
		for (auto v = v1.begin();v < v1.end(); v++)
			cout << *v << " ";
		cout << endl;
		cout << "size:" << v1.cend()-v1.cbegin() << endl;
	}
	else cout << "noll" << endl;
	vector<int> v2(10);
	if (v2.begin() != v2.end())
	{
		for (auto v = v2.begin(); v < v2.end(); v++)
			cout << *v << " ";
		cout << endl;
		cout << "size:" << v2.cend() - v2.cbegin() << endl;
	}
	else cout << "noll" << endl;

	vector<int> v3(10, 42);
	if (v3.begin() != v3.end())
	{
		for (auto v = v3.begin(); v < v3.end(); v++)
			cout << *v << " ";
		cout << endl;
		cout << "size:" << v3.cend() - v3.cbegin() << endl;
	}
	else cout << "noll" << endl;

	vector<int> v4{ 10 };
	if (v4.begin() != v4.end())
	{
		for (auto v = v4.begin(); v < v4.end(); v++)
			cout << *v << " ";
		cout << endl;
		cout << "size:" << v4.cend() - v4.cbegin() << endl;
	}
	else cout << "noll" << endl;

	vector<int> v5{ 10, 42 }; 
	if (v5.begin() != v5.end())
	{
		for (auto v = v5.begin(); v < v5.end(); v++)
			cout << *v << " ";
		cout << endl;
		cout << "size:" << v5.cend() - v5.cbegin() << endl;
	}
	else cout << "noll" << endl;

	vector<string> v6{ 10 }; 
	if (v6.begin() != v6.end())
	{
		for (auto v = v6.begin(); v < v6.end(); v++)
			cout << *v << " ";
		cout << endl;
		cout << "size:" << v6.cend() - v6.cbegin() << endl;
	}
	else cout << "noll" << endl;

	vector<string> v7{ 10, "hi" }; 
	if (v7.begin() != v7.end())
	{
		for (auto v = v7.begin(); v < v7.end(); v++)
			cout << *v << " ";
		cout << endl;
		cout << "size:" << v7.cend() - v7.cbegin() << endl;
	}
	else cout << "noll" << endl;
	
	return 0;
}

在这里插入图片描述

练习题3.25

使用迭代器划分成绩(93页)

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
		vector<int>i(11, 0);
		int s;
		while (cin >> s)
		{
			if (s <= 100)
				++* (i.begin() + s / 10);
		}
		for (auto j : i)
			cout << j << ' ';
		cout << endl;
		
	return 0;
}

在这里插入图片描述

练习题3.26

二分搜索中,为什么用的是mid=beg+(end-beg)/2,而非mid=(beg+end)/2;?

end成员返回的迭代器是尾后迭代器,是指向尾元素的下一个位置P(95)

练习题3.27

假设txt_size是一个无参数的函数,返回值为int,下面定义是否合法?说明原因

unsigned buf_size = 1024;
(a )int ia[buf_size]; //不合法,buf_size不是常量
(b )int ia[4*7-14];// 合法的,4*7-14是个常量。
(c )int ia[txt_size()]; //不合法,函数返回值不是常量
(d )char str[11] = "fundamental"; //不合法,没有空间可放空字符

练习题3.28

下列数组中元素的值是什么?

string sa[10]; //string类型数组
int ia[10]; // 含有10个整数的数组,内容未知
int main()
{
    string sa2[10]; //string类型数组
    int ia2[10]; //含有10个整数的数组,内容未知
    for (auto i : ia2) {
        cout << i << " "; 
    }
}  

对于内置类型,定义在所有函数值之外会默认初始化,定义在函数内会导致未定义。

练习题3.29

相比vector来说,数组有哪些缺点?

大小固定,不够灵活。(P101)

练习题3.30

指出下面代码中索引的错误

constexpr size_t array_size = 10;
int ia[array_size]; 
for (size_t ix = 1; ix <= array_size; ix ++) { // 下标范围为0--9,而不是1--10,因此会越界
    ia[ix] = ix;
}

练习题3.31

编写程序,定义一个长度为10的int数组,令每个元素的值就是其下标值。

#include <iostream>
#include <string>
#include <vector>
using namespace std;

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

在这里插入图片描述

在这里插入图片描述

练习题3.32

将上题创建的数组拷贝给另一个数组, 使用vector重新程序。

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
	int a[10], b[10];
	for (int i = 0; i < 10; i++)a[i] = i;
	for (int j = 0; j < 10; j++)b[j] = a[j];
	for (auto c : b)cout << c << ' ';
	cout << endl;

	vector<int>v1;
	vector<int>v2;
	for (int k = 0; k < 10; k++)v1.push_back(k);
	v2 = v1;
	for (auto d : v2)cout << d << " ";
	cout << endl;	
	
	return 0;
}

练习题3.33

对于P10页程序而言,如果不初始化scores将会发生什么?

如果不初始化scores,则其中内容未知,可能为空,也可能有别的数据。

练习题3.34

假定p1和p2指向同一数组中的元素,则下面程序的功能是什么?什么情况下该程序是非法的?

p1 += p2 - p1; p2-p1为p1与p2之间的距离,p1+(p1和p2之间的距离),即最后得到p2的值。当p1或p2不在同一数组内,则程序非法。

练习题3.35

编写一段程序,利用指针将数组中的元素置为0

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
	int a[] = { 0,1,2,3,4,5,6,7,8,9 };
	int* p = a;
	for (int i = 0; i < end(a)- begin(a); i++)
	{
		*(p + i) = 0;
		cout << *(p + i)<<' ';
	}
	cout << endl;	
	return 0;
}

在这里插入图片描述

练习题3.36

编写一段程序,比较两个数组是否相等。再写一段程序,比较两个vector对象是否相等。

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
	int a[10] = { 0,1,2,3,4,5,6,7,8,9 };
	int b[10] = { 0,1,2,3,4,5,6,7,8,9 };
	int c[10] = { 1,1,2,3,4,5,6,7,8,9 };
	if(end(a) - begin(a)!= end(b) - begin(b))cout << "数组a与数组b不相等" << endl;
	else {
		for (int i = 0; i < end(a) - begin(a); i++)
		{
			if (a[i] != b[i])cout << "数组a与数组b不相等" << endl;
			break;
		}
		cout << "数组a与数组b相等" << endl;
	}	
	if (end(a) - begin(a) != end(c) - begin(c))cout << "数组a与数组c不相等" << endl;
	else {
		int j = 1;
		for (int i = 0; i < end(a) - begin(a); i++)if (a[i] != c[i])j = 0;
		if(j==0)cout << "数组a与数组c不相等" << endl;
		else cout << "数组a与数组c相等" << endl;
	}

	vector<int>v1 = {1,2,3,5,8 };
	vector<int>v2 = {1,2,3,5,8 };
	vector<int>v3 = {2,2,3,5,8 };

	if (v1 != v2)cout << "v1!=v2"<<endl;
	else cout << "v1=v2" << endl;
	if (v1 != v3)cout << "v1!=v2" << endl;
	else cout << "v1=v3" << endl;
	return 0;
}

在这里插入图片描述

练习题3.37

下面的程序是何含义,输出结果是什么?

	const char s[] = {'h', 'e', 'l', 'l', 'o'};
    const char* p = s;

    while (*p) {
        cout << *p << " ";
        ++ p;
    }
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
	const char s[] = {'h', 'e', 'l', 'l', 'o'};
    const char* p = s;

    while (*p) {
        cout << *p << " ";
        ++ p;
    }	
	return 0;
}

在这里插入图片描述

练习题3.38

两个指针相加不仅是非法的,并且没什么意义,请问为什么没有意义?

两个指针相加,相当于两个地址值相加,没有意义。

练习题3.39

写一段程序,比较两个string对象,再编写一段程序,比较两个c风格字符串的内容。

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
	  //对两个C风格字符串进行比较
    char str1[80], str2[80];
    cout << "请输入两个字符串" << endl;
    cin >> str1 >> str2;
    //利用cstring头文件定义的strcmp函数比较大小
    auto result = strcmp(str1, str2);
    switch (result)
    {
    case 1:
        cout << "第一个字符串大于第二个字符串" << endl;
        break;
    case -1:
        cout << "第一个字符串小于第二个字符串" << endl;
        break;
    case 0:
        cout << "第一个字符串等于第二个字符串" << endl;
        break;
    default:
        cout << "未定义的结果" << endl;
        break;
    }
    
	return 0;
}

练习题3.40

编写一段程序,定义两个字符数组并用字符串字面值初始化它们,接着再定义一个字符数组,存放前两个数组连接后的结果。使用strcpy和strcat把前两个数组的内容拷贝到第三个数组中

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
	 //定义两个字符数组,用字符串字面值初始化
    char str1[] = "Welcome to ";
    char str2[] = "C++ family!";

    const decltype(strlen(str1)) n = 50;
    //strlen返回长度时空字符不计算在内
    char str[n];//存放前两个数组的连接结果
    strcpy_s(str, str1);//第一个字符串拷贝到结果字符串
    strcat_s(str, str2);//第二个字符串接到结果字符串中
    cout << str << endl;

	return 0;
}

练习题3.41

编写一段程序,用整型数组初始化一个vector对象。

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
	int arr[] = { 0,1,2,3,4,5 };
    vector<int> ivec(begin(arr), end(arr));
    for (auto v : ivec)
    {
        cout << v << " ";
    }
    cout << endl;

	return 0;
}

GitHub参考答案

https://github.com/Sunfann/C-_Primer_key

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值