您的位置:首页 > 编程语言 > C语言/C++

《C++ Primer》第五版课后习题解答_第三章(2)(06-20)

2017-08-24 14:50 525 查看
系统环境: windows 10 1703

编译环境:Visual studio 2017

3.6

#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::cin;
using std::string;

int main()
{
string s;
cout << "Enter one string: " << endl;
getline(cin, s);
for (auto &c : s)
{
c = 'x';
}
cout << s << endl;
return 0;
}
最开始程序出现了问题,报错在15行。查询后发现,原因在于,当时的赋值语句右侧写成了「“x”」,此时 x 是字符串,将字符串赋给字符变量 c,就出现了错误。把双引号改为单引号后,正确编译。

3.7

将循环控制变量改为 char,编译运行结果保持不变。验证后发现确实不变。

3.8

#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::cin;
using std::string;

// 传统 for 循环
int main()
{
string s;
cout << "Enter one string: " << endl;
getline(cin, s);
for (string::size_type i = 0; i < s.size(); ++i)
{
s[i] = 'x';
}
cout << s << endl;
return 0;
}
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::cin;
using std::string;

// while 循环
int main()
{
string s;
cout << "Enter one string: " << endl;
getline(cin, s);
string::size_type i = 0;
while ( i < s.size())
{
s[i] = 'x';
++i;
}
cout << s << endl;
return 0;
}
范围 for 语句最简便,可以直接循环 string 内的每一个字符,而不需要为下标单独声明一个变量。

3.9

合法,输出字符串 s 的第一个字符。

(英文原版文字为 Is it valid? If not, why not?  没有查到在 C++ 中 valid 和 legal 的区别。)

3.10

#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::cin;
using std::string;

int main()
{
string s;
getline(cin, s);
string result;
for (string::size_type i = 0; i< s.size(); ++i)
{
if (!ispunct(s[i]))
{
result += s[i];
}
}
cout << result << endl;
return 0;
}


3.11

c 的类型是 const char&

合法与否取决于循环体内的语句,如果不改变 c 的值则合法,否则不合法。

3.12

(a) 正确,ivec 的元素是 vector 对象;

(b) 错误,对象类型不同;

(c) 正确,svec 内有十个元素,均为 null;

3.13

(a) 空 (b) 10个 (c) 10个 (d) 1个 (e) 2个 (f) 10个 (g) 10个

3.14

#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::endl;
using std::cin;
using std::string;
using std::vector;

int main()
{
int i;
vector<int> ivec;
while (cin >> i)
{
ivec.push_back(i);
}
for (int i = 0; i <= (ivec.size() - 1); ++i)
{
cout << ivec[i] << endl;
}
return 0;
}


3.15

#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::endl;
using std::cin;
using std::string;
using std::vector;

int main()
{
string s;
vector<string> svec;
while (cin >> s)
{
svec.push_back(s);
}
for (int i = 0; i <= (svec.size() - 1); ++i)
{
cout << svec[i] << endl;
}
return 0;
}


3.16

3.13 回答正确

3.17

#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::endl;
using std::cin;
using std::string;
using std::vector;

int main()
{
vector<string> svec;
vector<string> result;
string s;
while (cin >> s)
{
svec.push_back(s);
}
for (auto &a : svec)
{
for (auto &b : a)
{
b = toupper(b);
}
}
for (auto &c : svec)
{
cout << c << endl;
}
return 0;
}


3.18

不合法,ivec 中不存在任何元素,所以无法用下标访问。

可将 ivec 定义为:vector<int> ivec(10);

3.19

#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::endl;
using std::cin;
using std::string;
using std::vector;

int main()
{
vector<int> ivec1(10, 42); //这个方法更好。对于元素个数已知,并且所有元素相同的 vector,直接赋值的方法更快捷
vector<int> ivec2{ 42, 42, 42, 42, 42, 42, 42 ,42 ,42 ,42 };
vector<int> ivec3;
for (int i = 0; i <= 9; ++i)
{
ivec3.push_back(42);
}
return 0;
}


3.20

#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::endl;
using std::cin;
using std::string;
using std::vector;

// 相邻整数的和
int main()
{
vector<int> ivec;
int i;
int sum;
while (cin >> i)
{
ivec.push_back(i);//构建 vector
}
for (auto &a : ivec)
{
cout << a << endl;
}
for (vector<int>::size_type j = 0; j <= (ivec.size() - 2); ++j) // 从第一个数开始,求其和后一个数之和。所以j 取倒数第二个数的下标
{
cout << ivec[j] + ivec[j + 1] << endl;
}
return 0;
}
#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::endl;
using std::cin;
using std::string;
using std::vector;

// 两侧至中的和
int main()
{
vector<int> ivec;
int i;
int sum;
while (cin >> i)
{
ivec.push_back(i);
}
for (auto &a : ivec)
{
cout << a << endl;
}
for (vector<int>::size_type j = 0; j <= (ivec.size() - 1); ++j) // 从最左侧的数开始取,和相应的数相加
{
if (j < (ivec.size() - 1 - j)) //如果出现大于的情况,则表示取值越过了中线
{
cout << ivec[j] + ivec[(ivec.size() - 1 - j)] << endl;
}
else if (j == ((ivec.size() - 1 - j))) //如果相等,则表示取到了中间数
{
cout << "This is the middle number: " << ivec[j] << endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: