【C++Primer】第五版【学习笔记】习题解答第三章

ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考。

作者:cosefy

Date: 2020/1/10

第三章:字符串,向量和数组

练习3.2:

#include<iostream>
#include<string>
using std::string;
using std::cout;
using std::cin;
using std::endl;
int main()
{
string line;
//while (cin >> line) //每次输入一个词
while (getline(cin,line)) //每次输入一行
if (!line.empty())
cout << line << endl;
return 0;
}

练习3.3:

string的输入从字符开始,直到遇到空白停止;而getline遇到空白不停止,遇到回车停止。

练习3.4:

实现:比较输入的两个字符串是否等长,如果不等长,输出长度较大的那个字符串。

#include<iostream>
#include<string> using namespace std; int main()
{
string l1, l2;
cin >> l1 >> l2;
if (l1 == l2)
cout << "字符串相等" << endl;
else
if (l1.size() > l2.size())
cout << l1 << endl;
else if(l1.size() < l2.size())
cout << l2 << endl;
else
cout << "长度相等" << endl;
return 0;
}

练习3.5:

int main()
{
string l1, l2;
while (cin >> l2)
l1 = l1 + l2 + " ";
cout << l1 << endl;
return 0;
}

练习3.6:

实现:使用for语句,将字符串内的所有字符用X代替.

#include<iostream>
#include<string>
#include<ctype.h> using namespace std; int main()
{
string s;
getline(cin,s);
for (auto &c : s)
if (isalpha(c))
c = 'X';
cout << s << endl;
return 0;
}

练习3.8:

for循环更好,形式更加简洁。

练习3.9:

不合法,因为字符串s没有初始化,直接访问第一个字符会发生未知错误。

练习3.10:

实现:读入一行含标点符号的字符串,将标点符号删除后输出剩余部分

#include<iostream>
#include<string>
#include<ctype.h> using namespace std; int main()
{
string s,s1;
getline(cin, s);
for (auto& c : s)
if (!ispunct(c))
s1 = s1 + c;
cout << s1<<endl;
return 0;
}

练习3.12:

  • a正确
  • b错误,sevc是string类型,ivec是int型,类型不匹配。
  • c错误,应该用花括号来处理。

练习3.13:

  • v1:一个元素,空值
  • v2:10个元素都为0
  • v3:10个元素都为42
  • v4:一个元素,值为10
  • v5:两个元素,分别为10,42
  • v6:10个元素,都为空字符串
  • v7:10个元素,都为"hi"

练习3.14:

实现:用cin读入一组整数,并把它们存入一个vector对象

#include<iostream>
#include<string>
#include<vector> using namespace std; int main()
{
vector<int> invc;
int a;
while (cin >> a)
invc.push_back(a);
return 0;
}

练习3.17:

实现:cin读入一组词,把它们存入一个vector对象,然后设法把所有词改为大写形式,每个词占一行。

#include<iostream>
#include<string>
#include<vector> using namespace std; int main()
{
vector<string> vctr;
string s;
decltype(vctr.size()) index = 0;
while (cin >> s)
{
for (auto& c : s)
c = toupper(c);
vctr.push_back(s);
cout << vctr[index] << endl;
index += 1;
}
return 0;
}

练习3.18:

不合法,vector对象的下标只能用来访问已存在的元素,而不能用来添加元素。

可以用ivec.push_back()函数来添加。

练习3.19:

vector<int> v1(10,42)   //第一种
vector<int>v1={42,42,42,42,42,42,42,42,42,42} //第二种
vector<int>v2; //第三种
for(int decltype(v1.size()) i=0; i!=10;i++)
v1.push_back(42);

练习3.20:

实现:读入一组整数到vrctor对象中,要求先输出第一个和最后一个整数的和,然后输出第二个和倒数第二个整数的和,以此类推。

#include<iostream>
#include<string>
#include<vector> using namespace std; int main()
{
vector<int>v1;
int value;
while (cin >> value)
v1.push_back(value);
for (decltype(v1.size())i = 0; i <= v1.size() / 2; i++)
if (v1.size() % 2 != 0 && i == v1.size() / 2)
cout << v1[i] << endl;
else
cout << v1[i]+v1[v1.size()-1-i] << endl; return 0;
}

练习3.22:

实现:把text的第一部分改为大写,然后输出。

#include<iostream>
#include<string>
#include<ctype.h> using namespace std; int main()
{
string text ("hello world");
for (auto it = text.begin(); it != text.end() && !isspace(*it); ++it)
*it = toupper(*it);
cout << text << endl;
return 0;
}

练习3.23:

实现:创建一个含有10个整数的vector对象,用迭代器将所有元素的值变为原来的两倍,最后输出vector对象的内容。

#include<iostream>
#include<string>
#include<vector> using namespace std; int main()
{
vector<int>v1;
int t = 0;
for (unsigned i = 0; i != 10; i++)
{
cin >> t;
v1.push_back(t); //创建含有10个整数的vector对象
}
for (auto it = v1.begin(); it != v1.end(); it++)
{
*it = *it * *it; //用迭代器处理
cout << *it<<" ";
}
return 0;
}

练习3.28:

函数体内的string型数组sa2与函数体内的string型数组sa,元素都是空。

函数体内的int型数组ia2中的元素未初始化;函数体外的int型数组ia中的元素都是0.

练习3.35:

实现:利用指针将数组中的值置为0.

using namespace std;
int main()
{
int a[] = { 1,2,3,4,5,6 };
int* p = begin(a), * q = end(a);
while (p != q)
{
*p = 0;
cout << *p;
p++;
}
}

《C++Primer》第五版习题答案--第三章【学习笔记】的更多相关文章

  1. 《C++Primer》第五版习题答案--第六章【学习笔记】

    <C++Primer>第五版习题答案--第六章[学习笔记] ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/16 第六章:函数 ...

  2. C++Primer第五版——习题答案目录

    目前正在刷<C++Primer>这本书,会在博客上记录课后习题答案,答案仅供参考. 因为水平有限,如有有误之处,希望大家不吝指教,谢谢! 目录地址 使用的系统为:win 10,编译器:VS ...

  3. C++Primer第五版——习题答案详解(一)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第1章 开始&&第2章 变量和基本类型 练习1.3 #include&l ...

  4. C++Primer第五版——习题答案详解(二)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第3章 字符串.向量和数组 练习3.2 一次读入一整行 #include<iost ...

  5. C++Primer第五版——习题答案详解(三)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第4章 表达式 练习4.10 while(cin>>i&&i ...

  6. C++Primer第五版——习题答案详解(四)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第5章 语句 练习5.9 #include<iostream> #inclu ...

  7. C++Primer第五版——习题答案详解(五)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第6章 函数 练习6.4 #include<iostream> using ...

  8. C++Primer第五版——习题答案详解(六)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第7章 类 练习7.1 class Sales_data { public: std:: ...

  9. C++Primer第五版——习题答案详解(七)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第8章 IO库 练习8.1 istream &iofunc(istream &a ...

随机推荐

  1. vbox ubuntu虚拟机中加载笔记本内置摄像头

    使用C:\Program Files\Oracle\VirtualBox\VBoxManage.exe工具加载摄像头 1,显示可用摄像头 C:\Program Files\Oracle\Virtual ...

  2. SuperSocket命令程序集定义

    是的,SuperSocket是用反射来查找哪些公开的类实现了基本的命令接口,但是它只在你的AppServer类定义的程序集中查找. 举例来说, 你的 AppServer 定义在程序集 GameServ ...

  3. 手机QQ浏览器属于代理服务器吗?

    这两天.上QQ,会员上线提示.老是显示福建省,而没有具体的地方.这是怎么回事呢?而且那个时间段我都没有上QQ.但是有用手机QQ浏览器.偷菜.这是怎么回事,机子也没有病毒 没有木马 到底怎么搞的...! ...

  4. jQuery的引入和使用

    https://www.cnblogs.com/sandraryan/ 前端代码优化:无效循环越少越好,DOM节点操作越少越好,HTTP请求越少越好 jq是一个js库.(不是框架) JQ优点 1. 方 ...

  5. java代码注释:单行//,多行/* */,文档注释/** */

    1.单行注释      //: //后到本行结束的所有字符会被编译器忽略; 2.多行注释     /* */: /*  */之间的所有字符会被编译器忽略 3.文档注释     /** */: 在/** ...

  6. jackson中的@JsonBackReference和@JsonManagedReference,以及@JsonIgnore

    jackson中的@JsonBackReference和@JsonManagedReference,以及@JsonIgnore均是为了解决对象中存在双向引用导致的无限递归(infinite recur ...

  7. H3C 不同匹配顺序导致结果不同

  8. java.lang.ClassCastException: com.sun.proxy.$Proxy6 cannot be cast to com.etc.service.serviceImpl.BankServiceImpl

    错误原因: java.lang.ClassCastException: com.sun.proxy.$Proxy6 cannot be cast to com.etc.service.serviceI ...

  9. 机器学习——EM

    整理自: https://blog.csdn.net/woaidapaopao/article/details/77806273?locationnum=9&fps=1 EM算法是用于含有隐变 ...

  10. linux 安装一个中断处理

    如果你想实际地"看到"产生的中断, 向硬件设备写不足够; 一个软件处理必须在系统中配 置. 如果 Linux 内核还没有被告知来期待你的中断, 它简单地确认并忽略它. 中断线是一个 ...