C++Primer第三章习题
来源:互联网 发布:网络升级紧急通知 编辑:程序博客网 时间:2024/04/29 08:25
【3.2】编写一段程序从标准输入中一次读入一整行,然后修改该程序使其一次读入一个词。
#include<iostream>#include<string>using namespace std;void main(){ string line,word; if(getline(cin,line)) cout<<line<<endl; if(cin>>word) cout<<word<<endl;}
【3.4】编写一段程序读入两个字符串,比较其是否相等并输出结果。如果不相等,输出较大的那个字符串。改写上述程序,比较输入的两个字符串是否等长,如果不等长,输出长度较大的那个字符串。
#include<iostream>#include<string>using namespace std;void main(){ string s1,s2,temp; cin>>s1>>s2; if(s1!=s2) temp=s1>s2?s1:s2; cout<<temp;}
#include<iostream>#include<string>using namespace std;void main(){ string s1,s2,temp; cin>>s1>>s2; if(s1.size()!=s2.size()) temp=s1.size()>s2.size()?s1:s2; cout<<temp;}
【3.5】编写一段程序从标准输入中读入多个字符串并将它们连接在一起,输出连接成的大字符串。然后修改上述程序,用空格把输入的多个字符串分隔开来。
#include<iostream>#include<string>using namespace std;void main(){ string s1,s2; while(cin>>s2) s1=s1+" "+s2; cout<<s1;}
【3.6】编写一段程序,使用范围for语句将字符串内的所有字符用X代替。
#include<iostream>#include<string>using namespace std;void main(){ string str; cin>>str; for(auto c:str.size()) str[c]='X'; cout<<str<<endl;}
【3.10】编写一段程序,读入一个包含标点符号的字符串,将标点符号去除后输出字符串剩余的部分。
#include<iostream>#include<cctype>#include<string>#include<cstdlib>using namespace std;void main(){ string str; cout<<"Enter your string(hit ENTER when finished):"<<endl; if(getline(cin,str)) { int i; for(i=0;i<str.size();i++) { if(ispunct(str[i])) { int j; for(j=i;j<str.size()-1;j++) str[j]=str[j+1]; str.erase(str.end()-1); } } } cout<<"The string without punctuation is : "<<str<<endl;}
【3.17】从cin读入一组词并把它们存入一个Vector对象,然后设法把所有词都改写为大写形式。输出改变后的结果,每个词占一行。
#include <iostream>#include <string>#include <vector>using namespace std;int main(){ vector<string> V; for(string t; cin >> t; V.push_back(t)); for(auto &str : V) for(auto &ch : str) ch = toupper(ch); for(auto i=V.begin(); i!=V.end(); ++i) cout << *i << endl; return 0;}
【3.20】读入一组整数并把它们存入一个vector对象,输出第1个和最后1个元素的和,接着输出第2个和倒数第2个元素的和,以此类推。
#include<iostream>#include <vector>#include <string>using namespace std;using namespace std; int main(){ vector<int> V; for(int t; cin>>t; V.push_back(t)); auto s = V.begin(); auto e = --V.end(); for(;s<=e; ++s, --e) if(s == e) cout << *s << endl; else cout << *s + *e << endl; return 0;}
【3.23】编写一段程序,创建一个含有10个整数的vector对象,然后使用迭代器将所有元素的值都变成原来的两倍。输出vector对象的内容,检验程序是否正确。
#include<iostream>#include <vector>#include <string>using namespace std;int main(){ vector<int> V(10); for(int i=0;i<10;i++) { cin>>V[i]; V[i]*=2; cout<<V[i]<<endl; } return 0;}
【3.41】编写一段程序,用整型数组初始化一个vector对象。
#include<iostream>#include <vector>using namespace std; int main(){ int ca[]={1,2,3,3,4}; vector<int> ivec(begin(ca),end(ca)); for(int i=0;i<ivec.size();i++) cout<<ivec[i]; return 0;}
【3.42】编写一段程序,将含有整数元素的vector对象拷贝给一个整型数组。
#include<iostream>#include <vector>using namespace std; int main(){ vector<int> intvect; int n=0; while(cin>>n) { intvect.push_back(n); } int *pa=new int[intvect.size()](); for(vector<int>::iterator iter=intvect.begin();iter!=intvect.end();iter++) { *pa=*iter; cout<<*pa<<endl; pa++; } return 0;}
0 0
- C++Primer第三章习题
- C++primer第三章习题3.7
- c++primer第三章习题(1)
- c++primer第三章习题(2)
- c++primer 第三章习题(3)
- c++primer 第三章习题(4)
- C++Primer第三章习题(1)
- C++Primer第三章习题(2)
- C++primer[习题][第三章][1-10]
- C++primer[习题][第三章][11-20]
- C++primer[习题][第三章][21-30]
- C++primer[习题][第三章][31-45]
- C++Primer 中文版 第五版 第三章课后习题答案
- 《C Primer Plus 》第六版 习题 第三章
- C++Primer第三版0316习题
- C++Primer第三版习题0325
- C++Primer第三版习题0326
- C++Primer第三版习题0413
- React.js系列学习
- Windows下dnspython安装
- Python 3.x 学习 —— 递归函数
- 笔记——Java之注解
- openVswitch(OVS)实现之源代码分析之工作流程
- C++Primer第三章习题
- 排除jar程序
- MFC 在父对话框中内嵌子对话框
- MOBX的入门案例
- (0035) iOS 开发之获得当前设备的IP(分IPv4 & IPv6)
- Ogre中获取mesh顶点数据
- [BZOJ3529][Sdoi2014]数表(莫比乌斯反演+树状数组)
- OC-百度地图计算两个坐标之间的距离
- CSS3选择器