C++ Primer第五版_第三章习题答案(11~20)
创始人
2025-05-29 10:12:16
0

文章目录

      • 练习3.11
      • 练习3.12
      • 练习3.13
      • 练习3.14
      • 练习3.15
      • 练习3.16
      • 练习3.17
      • 练习3.18
      • 练习3.19
      • 练习3.20

练习3.11

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

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

要根据for循环中的代码来看是否合法,c是string 对象中字符的引用,s是常量。因此如果for循环中的代码重新给c赋值就会非法,如果不改变c的值,那么合法。

练习3.12

下列vector对象的定义有不正确的吗?如果有,请指出来。对于正确的,描述其执行结果;对于不正确的,说明其错误的原因。

vector> ivec;         // 在C++11当中合法
vector svec = ivec;       // 不合法,类型不一样
vector svec(10, "null");  // 合法

练习3.13

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

vector v1;         // size:0,  no values.
vector v2(10);     // size:10, value:0
vector v3(10, 42); // size:10, value:42
vector v4{ 10 };     // size:1,  value:10
vector v5{ 10, 42 }; // size:2,  value:10, 42
vector v6{ 10 };  // size:10, value:""
vector v7{ 10, "hi" };  // size:10, value:"hi"

练习3.14

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

#include 
#include 
#include 
#include using std::cin;
using std::cout;
using std::endl;
using std::vector;int main()
{vector v;int i;while (cin >> i){v.push_back(i);}return 0;
}

练习3.15

改写上题程序,不过这次读入的是字符串。

#include 
#include 
#include 
#include using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;int main()
{vector v;string i;while (cin >> i){v.push_back(i);}return 0;
}

练习3.16

编写一段程序,把练习3.13中vector对象的容量和具体内容输出出来

#include 
#include 
#include 
#include using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;int main()
{vector v1;         // size:0,  no values.vector v2(10);     // size:10, value:0vector v3(10, 42); // size:10, value:42vector v4{ 10 };     // size:1,  value:10vector v5{ 10, 42 }; // size:2,  value:10, 42vector v6{ 10 };  // size:10, value:""vector v7{ 10, "hi" };  // size:10, value:"hi"cout << "v1 size :" << v1.size() << endl;cout << "v2 size :" << v2.size() << endl;cout << "v3 size :" << v3.size() << endl;cout << "v4 size :" << v4.size() << endl;cout << "v5 size :" << v5.size() << endl;cout << "v6 size :" << v6.size() << endl;cout << "v7 size :" << v7.size() << endl;cout << "v1 content: ";for (auto i : v1){cout << i << " , ";}cout << endl;cout << "v2 content: ";for (auto i : v2){cout << i << " , ";}cout << endl;cout << "v3 content: ";for (auto i : v3){cout << i << " , ";}cout << endl;cout << "v4 content: ";for (auto i : v4){cout << i << " , ";}cout << endl;cout << "v5 content: ";for (auto i : v5){cout << i << " , ";}cout << endl;cout << "v6 content: ";for (auto i : v6){cout << i << " , ";}cout << endl;cout << "v7 content: ";for (auto i : v7){cout << i << " , ";}cout << endl;return 0;
}

练习3.17

从cin读入一组词并把它们存入一个vector对象,然后设法把所有词都改为大写形式。输出改变后的结果,每个词占一行。

#include 
#include 
#include 
#include using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;int main()
{vector v;string s;while (cin >> s){v.push_back(s);}for (auto &str : v){for (auto &c : str){c = toupper(c);}}for (auto i : v){cout << i << endl;}return 0;
}

练习3.18

下面的程序合法吗?如果不合法,你准备如何修改?

vector ivec;
ivec[0] = 42;

不合法。应改为:

ivec.push_back(42);

练习3.19

如果想定义一个含有10个元素的vector对象,所有元素的值都是42,请例举三种不同的实现方法,哪种方式更好呢?

如下三种:

vector ivec1(10, 42);
vector ivec2{ 42, 42, 42, 42, 42, 42, 42, 42, 42, 42 };
vector ivec3;
for (int i = 0; i < 10; ++i)ivec3.push_back(42);

第一种方式最好。

练习3.20

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

#include 
#include 
#include 
#include using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;int main()
{vector ivec;int i;while (cin >> i){ivec.push_back(i);}for (int i = 0; i < ivec.size() - 1; ++i){cout << ivec[i] + ivec[i + 1] << endl;}//---------------------------------cout << "---------------------------------" << endl;int m = 0;int n = ivec.size() - 1;while (m < n){cout << ivec[m] + ivec[n] << endl;++m;--n;}return 0;
}

相关内容

热门资讯

日媒曝光:日本曾制定3套“夺岛... 据央视新闻报道,随着日本首相高市早苗涉台挑衅言论持续发酵,日本自卫队在靠近台海的岛屿加强军力部署的情...
舞蹈家黄豆豆获破格提拔,已任副... 今年4月拟破格提拔的舞蹈家黄豆豆,已有新消息。 澎湃新闻注意到,中国舞蹈家协会官网近日更新后显示,黄...
李霄鹏告别青岛海牛:战术调整与... 随着2023赛季的落幕,李霄鹏教练组已正式与青岛海牛球员告别,确认下赛季将不再继续执教。这一决定不仅...
高市早苗狠话说早了,美国国会:... 日本首相高市早苗发表的“若台海有事,日本就可行使集体自卫权”的言论,完全就是刻意放狠话,对华发出战争...
法律顾问能处理保险法律事务吗 在当今复杂的商业和法律环境中,保险法律事务对于企业和个人都至关重要。许多人会疑惑,法律顾问是否有能力...
海南国际清算所:依托海南自贸港... 编者按 随着我国多层次的商品市场体系持续完善,场外衍生品市场发展加速推进,其服务实体经济的核心价值正...
威少21+6+11约基奇空砍4... 【搜狐体育战报】北京时间11月23日NBA常规赛,客场作战的国王以128-123击败掘金。威斯布鲁克...
跨境电商高发!多部门共织“天罗... 东方网11月23日消息:11月21日,上海市人民检察院第三分院与上海海关缉私局、上海海警局、上海市第...
法律顾问能处理旅游法律事务吗 在旅游行业蓬勃发展的当下,各类旅游法律事务也日益增多,许多人会疑惑法律顾问是否能处理旅游法律事务。答...
法律顾问能处理哪些法律事务 在当今复杂多变的商业环境中,企业面临着各种各样的法律风险和挑战,法律顾问的重要性日益凸显。那么,法律...