C++ Primer课后习题解答(第三章)
Exercises Section 3.2.2
Ex3.2
// 一次读取一行
#include<iostream>
#include<string>
using namespace std;
int main()
{
string line;
while (getline(cin, line))
{
cout << line << endl;
}
system("pause");
return 0;
}
// 一次读取一个 word
#include<iostream>
#include<string>
using namespace std;
int main()
{
string word;
while (cin >> word)
{
cout << word << endl;
}
system("pause");
return 0;
}
Ex3.3
在 string 输入操作中, whitespace characters 被忽略;在 getline 函数中,whitespace characters 正常输出。
Ex3.4
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1;
string s2;
cout << "Please enter two string: " << endl;
getline(cin, s1);
getline(cin, s2);
//比较 s1 和 s2 的大小
if (s1 == s2) // s1 和 s2 的长度和内容都相等
{
cout << "s1 and s2 are equal" << endl;
}
else if (s1 < s2)
{
cout << "The larger string is s2" << endl;
}
else
{
cout << "The larger string is s1" << endl;
}
// 比较 s1 和 s2 长度的大小
if (s1.size() == s2.size())
{
cout << "The length of s1 equals the length of s2" << endl;
}
else if (s1.size() < s2.size())
{
cout << "s2 is longer" << endl;
}
else
{
cout << "s1 is longer" << endl;
}
system("pause");
return 0;
}
Ex3.5
// 拼接 string
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
string result = "";
while (getline(cin, s))
{
result += s;
}
cout << result << endl;
system("pause");
return 0;
}
// 分别输出 string
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
string s;
vector<string> result;
while (getline(cin, s))
{
result.push_back(s);
}
for (string i : result)
{
cout << i << " ";
}
cout << endl;
system("pause");
return 0;
}
Exercises Section 3.2.3
Ex3.6
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
getline(cin, s);
for (auto &c : s)
{
c = 'X';
}
cout << s << endl;
system("pause");
return 0;
}
Ex3.7
不能得到预期的结果,这是因为首先复制了 string 的一个副本,只是对其副本进行了修改。
Ex3.8
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
getline(cin, s);
auto it = s.begin();
while (it != s.end())
{
(*it) = 'X';
++it;
}
cout << s << endl;
system("pause");
return 0;
}
Ex3.9
string s;
cout << s[0] << endl;
输出 string s 的第一个字符;有效
Ex3.10
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
getline(cin, s);
for (auto &c : s)
{
if (!ispunct(c))
{
cout << c;
}
}
cout << endl;
system("pause");
return 0;
}
Ex3.11
const string s = "Keep out!";
for (auto &c : s){ /* ... */ }
合法;c 的类型为 const char&
Exercises Section 3.3.1
Ex3.12
a) vector<vector<int>> ivec; // 合法;ivec 是一个 vector,里面的元素是 存储 int 类型的 vector
b) vector<string> svec = ivec; // 不合法; 利用一个 vector 给另外一个 vector 赋值,必须保证两个 vector 内的元素类型一样
c) vector<string> svec(10, "null"); // 合法;svec是一个 vector,有 10 个元素,每个元素为字符串值"null"
Ex3.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"
Exercises Section 3.3.2
Ex3.14
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int value;
vector<int> result;
while (cin >> value)
{
result.push_back(value);
}
for (auto i : result)
{
cout << i << " ";
}
cout << endl;
system("pause");
return 0;
}
Ex3.15
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
string value;
vector<string> result;
while (getline(cin, value))
{
result.push_back(value);
}
for (auto s : result)
{
cout << s << " ";
}
cout << endl;
system("pause");
return 0;
}
Exercises Section 3.3.3
Ex3.17
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
string word;
vector<string> result;
while (cin >> word)
{
result.push_back(word);
}
for (auto & s : result)
{
for (auto &c : s)
{
c = toupper(c);
}
}
for (auto i = 0; i != result.size(); ++i)
{
if (i % 8 != 0)
cout << result[i] << " ";
else
{
cout << endl;
cout << result[i] << " ";
}
}
cout << endl;
system("pause");
return 0;
}
Ex3.18
vector<int> ivec;
ivec[0] = 42;
不合法;修改如下:
vector<int> ivec;
ivec.push_back(42);
Ex3.19
vector<int> v1 = {42, 42, 42, 42, 42, 42, 42, 42, 42, 42};
vector<int> v2(10, 42);
vector<int> v3 = v2;
Ex3.20
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int value;
vector<int> result;
int sum = 0;
while (cin >> value)
{
result.push_back(value);
}
for (auto i = result.begin(), j = i + 1; i != result.end(); ++i)
{
sum = (*i) + (*j);
cout << sum << " ";
}
cout << endl;
for (auto i = result.begin(), j = result.end() - 1; i <= j; ++i, --j)
{
if (i != j)
{
sum = (*i) + (*j);
cout << sum << " ";
}
else
{
sum = (*i);
cout << sum << " ";
}
}
cout << endl;
system("pause");
return 0;
}
Exercises Section 3.4.1
Ex3.23
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int value;
vector<int> result;
for (int i = 0; i < 10; ++i)
{
cin >> value;
result.push_back(2 * value);
}
for (auto item = result.begin(); item != result.end(); ++item)
{
cout << *item << " ";
}
cout << endl;
system("pause");
return 0;
}
Exercises Section 3.4.2
Ex3.26
mid = beg + (end - beg) / 2; // 推荐
mid = (beg + end) / 2; // 不推荐
原因:第二种可能造成整数溢出
Exercises Section 3.5.1
Ex3.27
unsigned buf_size = 1024;
a) int ia[buf_size]; // 非法;buf_size 不是一个常量
b) int ia[4 * 7 - 14]; // 合法
c) int ia[txt_size()]; // 如果 txt_size() 返回的是常量 int 类型则合法
d) char st[11] = "fundamental"; // 非法;最后的'\0'占一个字符,故而数组大小不够
Ex3.28
string sa[10]; // 元素值全为空字符串
int ia[10]; // 元素值全为0
int main()
{
string sa2[10]; // 元素值全为空字符串
int ia2[10]; // 元素值不确定,取决于编译器
}
Ex3.29
array 大小确定,而 vector 大小可以不确定。
Exercises Section 3.5.2
Ex3.30
constexpr size_t array_size = 10;
int ia[array_size];
for (size_t ix = 1; ix <= array_size; ++ix) // 下标为0-9,没有 10 下标
ia[ix] = ix;
Ex3.31
#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] << " ";
}
cout << endl;
system("pause");
return 0;
}
Ex3.32
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int a[10], b[10];
vector<int> s1;
vector<int> s2;
for (int i = 0; i < 10; ++i)
{
a[i] = i;
}
for (int i = 0; i < 10; ++i)
{
b[i] = a[i];
}
for (auto i = 0; i < 10; ++i)
{
s1.push_back(i);
}
for (vector<int>::const_iterator it = s1.begin(); it != s1.end(); ++it)
{
s2.push_back(*it);
}
for (vector<int>::const_iterator it = s2.begin(); it != s2.end(); ++it)
{
cout << *it << " ";
}
cout << endl;
system("pause");
return 0;
}
Ex3.33
未初始化的数组的元素值取决于编译器
Exercises Section 3.5.3
Ex3.34
p1 += p2 - p1; // 将指针 p2 的值赋给指针 p1;如果 p1 被 const 修饰则非法
Ex3.35
#include<iostream>
using namespace std;
int main()
{
int a[10];
int *p = &a[10];
for (int *it = a; it != p; ++it)
{
*it = 0;
}
for (int x : a)
{
cout << x << " ";
}
cout << endl;
system("pause");
return 0;
}
Ex3.36
// 比较数组是否相等
#include<iostream>
using namespace std;
int main()
{
int arr1[10];
int arr2[10];
int size_arr1 = end(arr1) - begin(arr1);
int size_arr2 = end(arr2) - begin(arr2);
if (size_arr1 != size_arr2) {
cout << "Differen. " << endl;
return 0;
}
for (int i = 0; i != size_arr1; ++i) {
if (arr1[i] != arr2[i]) {
cout << "Different. " << endl;
return 0;
}
}
cout << "Same arr. " << endl;
system("pause");
return 0;
}
// 比较vector 是否相等
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> ivec1{1, 2, 3};
vector<int> ivec2{1, 2, 3};
if (ivec1 == ivec2)
{
cout << "Same vector. " << endl;
}
else
{
cout << "Different. " << endl;
}
system("pause");
return 0;
}
Exercises Section 3.5.4
Ex3.37
#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;
}
Ex3.38
C++ 中没有指针加法
Ex3.39
// C char[]
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
const char ca1[] = "string-A";
const char ca2[] = "string-B";
if (strcmp(ca1 , ca2) > 0)
{
cout << "string-A big" << endl;
}
else
{
cout << "string-B big" << endl;
}
system("pause");
return 0;
}
// C++ string
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1 = "string-A";
string s2 = "string-B";
cout << "Big one is: ";
if (s1 > s2)
{
cout << s1 << endl;
}
else
{
cout << s2 << endl;
}
system("pause");
return 0;
}
Ex3.40
#include<cstring>
#include<iostream>
using namespace std;
int main()
{
char s1[100] = "stringA";
char s2[] = "stringB";
strcat(s1 , s2);
char s[100];
strcpy(s , s1);
cout << s << endl;
system("pause");
return 0;
}
Exercises Section 3.5.5
Ex3.41
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int value;
vector<int> v1;
int a[10];
for (int i = 0; i < 10; ++i)
{
cin >> value;
a[i] = value;
}
for (int i = 0; i < 10; ++i)
{
v1.push_back(a[i]);
}
for (auto i : v1)
{
cout << i << " ";
}
cout << endl;
system("pause");
return 0;
}
Ex3.42
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int value;
vector<int> v;
while (cin >> value)
{
v.push_back(value);
}
int a[v.size()];
for (int i = 0; i < v.size(); ++i)
{
a[i] = v[i];
}
for (int i = 0; i < v.size(); ++i)
{
cout << a[i] << " ";
}
system("pause");
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端