概述
练习3-2 编写程序实现从标准输入每次读入一行文本。然后改写程序,每次读入一个单词。
#include <iostream>
#include <string>
using namespace std;
void test3_2_1(){
string line;
//一次读入一行 直到文件结束
while(getline(cin,line)){
cout<<line<<endl;//输出相应行以验证
}
}
void test3_2_2(){
string word;
//一次读入一个单词 直到文件结束
while(cin>>word)
cout<<word<<endl;
}
int main(){
test3_2_1();
//test3_2_2();
return 0;
}
练习3-3 解释string 类型的输入操作符和getline 函数分别如何处理空白字符。
string类型的输入操作符对空白字符的处理:读取并忽略有效字符(非空白字符)之前所有的空白字符,然后读取字符直至再次遇到空白字符,读取终止(该空白字符仍留在输入流中)。getline函数对空白字符的处理:不忽略行开头的空白字符,读取字符直至遇到换行符,读取终止并丢弃换行符(换行符从输入流中去掉但并不存储在string对象中)。
练习3-4 编一个程序读入两个string 对象,测试它们是否相等。若不相等,则指出两个中哪个较大。接着,改写程序测试它们的长度是否相等,若不相等,则指出两个中哪个较长。
void test3_4(){
string one;
string two;
// input one
//input two
getline(cin,one);
getline(cin,two);
if(one == two){
cout<<"they are same"<<endl;
}else{
cout<<"they are not same"<<endl;
}
if(one.size()==two.size()){
cout<<"they have same size"<<endl;
}else{
cout<<"they dont have same size"<<endl;
}
}
练习3-5
编一个程序,从标准输入读取多个string 对象,把它们连接起来存放到一个更大的string 对象中,并输出连接后的string 对象。接着,改写程序,将连接后相邻string 对象以空格隔开。
void test3_5(){string line;
string result;
//一次读入一行 直到文件结束
while(getline(cin,line)){
cout<<line<<endl;//输出相应行以验证
result +=line;
result +=" ";//test3_5
}
cout<< result<<endl;
}
int main(){
//test3_2_1();
//test3_2_2();
//test3_4();
test3_5();
return 0;
}
练习3-6编写一段程序,使用范围for循环将字符串内的所有字符用X代替。
练习3-7若将上一题的循环控制变量替换为char,又有什么效果,编程验证。
void test3_6(){
string str = "123456";
for (auto &c : str) c = 'X';
cout << str << " ";
}
void test3_7(){
string str = "123456";
for (char &c : str) c = 'X';
cout << str << " ";
}
练习3-8分别用while和for重写上一题,你感觉哪个更好
显然,类型推断节省代码
void test3_7(){
string str = "123456";
for (char &c : str) c = 'X';
cout << str << " ";
}
void test3_8(){
string str = "123456";
for (size_t i = 0; i < str.size(); i++)
{
str[i] = 'X';
}
cout << str << endl;
str = "123456";
int i = 0;
while (i<str.size()){
str[i] = 'X';
i++;
}
cout << str << endl;
}
练习3-10编写一段程序,读入一个包含标点符号的字符串,然后将标点符号去除后输出字符串剩余部分。
void test3_10(){
string word;
string temp;
//一次读入一个单词 直到文件结束
while (cin >> word){
for (auto &c : word)
{
if (!ispunct(c)){
temp += c;
}
}
cout <<"word "<< word << endl;
cout <<"temp "<< temp << endl;
temp = "";
}
}
练习3-20 读入一组整数并把它们存入一个vector对象,将其相邻结果求和并输出。改写你的程序,每次把第一个和最后一个相加,第二个和倒数第二个相加,以此类推。
void test3_20(){
vector<int> len;
for (size_t i = 0; i < 10; i++)
{
len.push_back(i);
}
cout <<"i + i + 1 "<< endl;
for (size_t i = 0; i < len.size()-1; i++)
{
cout << len[i] + len[i + 1] << endl;
}
cout << "0 len" << endl;
for (size_t i = 0; i < len.size(); i++)
{
cout << len[i] + len[len.size() - 1 - i] <<endl;
}
}
练习3-21利用迭代器重做3.3.3节第一个练习
void test3_21(){
vector<int> len;
for (size_t i = 0; i < 10; i++)
{
len.push_back(i);
}
for (auto it = len.begin(); it != len.cend(); ++it){
cout << *it << endl;
}
}
练习3-22修改输出text程序,把第一段改成大写形式,再输出它
void test3_22(){
string word;
vector<string> text;
while (cin >> word){// the end is ctrl + z
text.push_back(word);
}
for (auto &j : text) {
for (auto it = j.begin(); it != j.end(); ++it){
*it = toupper(*it);
}
};
for (auto i : text) {
cout << i << endl;
};
}
练习3-23编写一段程序,创建一个包含10个整数的vector对象,使用迭代器使其为原来的两倍,输出内容,检查输出是否正确。
void test3_23(){
vector<int> len;
for (size_t i = 0; i < 10; i++)
{
len.push_back(i);
}
for (auto it = len.begin(); it != len.cend(); ++it){
*it *= 2;
}
for (auto it = len.begin(); it != len.cend(); ++it){
cout << *it << endl;
}
}
Exercise3.34
Given that p1 and p2 point toelements in the same array, what does the following code do? Are there valuesof p1 or p2 that make this code illegal?
p1 += p2 - p1;
we assume p1 and p2 point to an array arr. so p1 = &arr[0]
; and p2 =&arr[0]
. p2 - p1 is the distance ofarr[0] to arr[0], and must be zero. so p1 += 0;
can not change the p1's point.
p1 += p2- p1;
same as p1 = p2;
. If p2 and p1 are legal, this code always legal.
Exercise3.35
#include <iostream> | |
#include <iterator> | |
using std::begin; | |
using std::end; | |
using std::cout; | |
using std::endl; | |
int main() | |
{ | |
int arr[10]; | |
int* b = begin(arr); | |
int* e = end(arr); | |
for (int* i = b; i != e; ++i) *i =0; | |
for (auto i : arr) cout << i <<""; | |
cout << endl; | |
return 0; | |
} |
Exercise3.36
#include <iostream> | |
#include <vector> | |
#include <iterator> | |
using std::begin; | |
using std::end; | |
using std::cout; | |
using std::endl; | |
using std::vector; | |
// pb point to begin of the array, pe point to end of the array. | |
bool compare(int* const pb1, int* const pe1, int* const pb2, int* const pe2) | |
{ | |
if ((pe1 - pb1) != (pe2 - pb2))// have different size. | |
return false; | |
else { | |
for (int *i = pb1, *j = pb2; (i != pe1) && (j != pe2); ++i, ++j) | |
if (*i != *j) return false; | |
} | |
return true; | |
} | |
int main() | |
{ | |
int arr1[3] = {0,1, 2}; | |
int arr2[3] = {0,2, 4}; | |
if (compare(begin(arr1),end(arr1), begin(arr2), end(arr2))) | |
cout << "The two arrays are equal." << endl; | |
else | |
cout << "The two arrays are not equal." << endl; | |
cout << "==========" << endl; | |
vector<int> vec1 = {0,1, 2}; | |
vector<int> vec2 = {0,1, 2}; | |
if (vec1 == vec2) | |
cout << "The two vectors are equal." << endl; | |
else | |
cout << "The two vectors are not equal." << endl; | |
return 0; | |
} |
Exercise3.42
#include <iostream> | |
#include <vector> | |
using std::vector; | |
using std::cout; | |
using std::endl; | |
using std::begin; | |
using std::end; | |
int main() | |
{ | |
vector<int> ivec{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; | |
int int_arr[10]; | |
for (int* i = begin(int_arr); i != end(int_arr); ++i) | |
*i = ivec[i - begin(int_arr)]; | |
for (auto i : int_arr) cout << i << " "; | |
cout << endl; | |
return 0; | |
} |
最后
以上就是柔弱百褶裙为你收集整理的C++Primer Chapter3课后习题的全部内容,希望文章能够帮你解决C++Primer Chapter3课后习题所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复