C++Primer第五版 ——— (ch3)课后习题参考答案
- 练习 3.1
- 练习 3.2
- 练习 3.3
- 练习 3.4
- 练习 3.5
- 练习 3.6
- 练习 3.7
- 练习3.8
- 练习 3.9
- 练习 3.10
- 练习 3.11
- 练习题3.12
- 练习题3.13
- 练习题3.14
- 练习题3.15
- 练习题3.16
- 练习题3.18
- 练习题3.19
- 练习题3.20
- 练习题3.21
- 练习题3.25
- 练习题3.26
- 练习题3.27
- 练习题3.28
- 练习题3.29
- 练习题3.30
- 练习题3.31
- 练习题3.32
- 练习题3.33
- 练习题3.34
- 练习题3.35
- 练习题3.36
- 练习题3.37
- 练习题3.38
- 练习题3.39
- 练习题3.40
- 练习题3.41
练习 3.1
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 函数
:
读入一整行,直到换行符为止,开头是换行符也读入换行符
停止。
练习 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
#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
练习3.8
#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
const string s = "Keep out!";
for(auto &c : s){ /* ... */ }
合法,c 是对 const char 的引用。但是c值不可改变!!!
练习题3.12
( a )vector<vector<int>> ivec;
( b )vector<string> svec = ivec;
( c )vector<string>svec(10, "null");
a:正确,创建了一个元素为vector的vector对象
b:不正确,类型不一致
c:正确,十个“null”对象
练习题3.13
( 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
#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
#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
#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
#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
#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
#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
end成员返回的迭代器是尾后迭代器,是指向尾元素的下一个位置P(95)
练习题3.27
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
大小固定,不够灵活。(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
#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
#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
如果不初始化scores,则其中内容未知,可能为空,也可能有别的数据。
练习题3.34
p1 += p2 - p1; p2-p1为p1与p2之间的距离,p1+(p1和p2之间的距离),即最后得到p2的值。当p1或p2不在同一数组内
,则程序非法。
练习题3.35
#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
#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
#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
#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;
}