C++ primer 第三章练习答案
标准库的string类型 和 字符串字面值不是一个东西
string.size() 的返回值类型是 string::size_type
3 .2
# include<iostream>
# include<string>
using namespace std;
int main()
{
string word;
while (cin >> word)
cout << word << endl;
return 0;
}
3.3 string 忽略第一个有效字符前的所有空格,遇到第一个有效字符开始输入, 遇到空格结束读取
getline 遇到空格继续读取,遇到回车结束读取
3.4
# include<iostream>
# include<string>
using namespace std;
// 比较两个字符串是否相等 输出结果并且输出较大的字符串
int main()
{
string str1, str2;
cout << "输入字符串1:" << endl;
cin >> str1;
cout << "输入字符串2:" << endl;
cin >> str2;
if (str1 == str2)
cout << "两个字符串相等" << endl;
else
cout << "俩个字符串不相等" << endl;
if (str1 > str2)
cout << "字符串 " << str1 << " 大" << endl;
else
cout << "字符串 " << str2 << " 大" << endl;
system("pause");
return 0;
}
3.5
原程序
# include<iostream>
# include<string>
using namespace std;
// ctrl + z 跳出循环
int main()
{
string tmp, answer;
while (cin >> tmp)
answer += tmp;
cout << "最后的字符串结果是:" << answer << endl;
system("pause");
return 0;
}
3.5 改
# include<iostream>
# include<string>
using namespace std;
// ctrl + z 跳出循环
int main()
{
string tmp, answer;
while (cin >> tmp)
answer =answer + ' ' + tmp;
cout << "最后的字符串结果是:" << answer << endl;
system("pause");
return 0;
}
3.6
# include<iostream>
# include<string>
using namespace std;
int main()
{
string str;
string tmp("x");
cout << "输入字符串:" << endl;
cin >> str;
for (auto &c : str)
c = 'x';
cout << "结果是:" << str<<endl;
system("pause");
return 0;
}
3.7
就是把上面c的类型变成char类型 发现str 不变成x
3.8
# include<iostream>
# include<string>
using namespace std;
int main()
{
string str;
string tmp("x");
cout << "输入字符串:" << endl;
cin >> str;
// i 应该用size_type类型
for (int i = 0; i < str.size(); i++)
str[i] = 'x';
/*
while( i <= str.size())
{
str[i] = 'x';
i++;
}
*/
cout << "结果是:" << str << endl;
system("pause");
return 0;
}
3.9
不合法,s未定义,s[0]没有值,造成不可预知的错误。
3.10
# include<iostream>
# include<string>
using namespace std;
int main()
{
string str;
cout << "请输入包含标点符号的字符串,我会把他们连接起来(必须是英文的标点):" << endl;
cin >> str;
for (int i = 0; i < str.size(); i++)
{
if (ispunct(str[i]))
{
for (int j = i; j < str.size(); j++)
str[j] = str[j + 1];
}
}
cout << "结果是:"<<str << endl;
system("pause");
return 0;
}
3.11
合法,c为char,是个引用
3.12
(a) 正确 ivec是一个 vector<int>类型的 vector
(b) 错误 svec不是 是istring类型
(c) 正确
3.13
(a) vector<int> v1; //不包含元素
(b) vector<int> v2(10); //v2包含十个元素每个元素都是0
(c) vector<int> v3(10, 42); //v3包含十个元素,每个元素都是42
(d) vector<int> v4{10}; //v4只有一个元素10
(e) vector<int> v5{10,42}; //v5有两个元素,10,42
(f) vector<string> v6{10}; //v6有十个元素,每个元素都是空串。 因为会首先尝试列表初始化,发现类型不一致,就会尝试值来构造vector。
(g) vector<string> v7{10, 'hi'}; // v7有十个元素,每个元素都是字符串"hi"。
突然觉得加一下题目比较好,以后自己复习的时候就不用翻书了
// 3.14 编写一段程序,用cin读入一组整数并把它们存入到一个vector对象
# include<iostream>
# include<vector>
using namespace std;
int main()
{
vector<int> num;
cout << "请输入一组整数:" << endl;
int intger;
while (cin >> intger)
num.push_back(intger);
cout << "结果是:" << endl;
for (auto &i : num)
cout << i << " ";
cout << endl;
system("pause");
return 0;
}
// 3.15 改写上面程序,不过这一次读入的是字符串
# include<iostream>
# include<vector>
# include<string>
using namespace std;
int main()
{
vector<string> str;
cout << "请输入一组字符串:" << endl;
string tmp;
while (cin >> tmp)
str.push_back(tmp);
cout << "结果是:" << endl;
for (auto &i : str)
cout << i << " ";
cout << endl;
system("pause");
return 0;
}
// 3.17 从cin读入一组此并把它们粗入一个vector对象,然后设法把所有词都改写为大写格式。输出改变后的结果,每个词占一行。 # include<iostream> # include<string> # include<vector> # include<cctype> using namespace std; int main() { vector<string> word_s; string word; cout << "请输入一组单词:"; while (cin >> word) word_s.push_back(word); for (auto &c : word_s) for (auto &tmp : c) tmp = toupper(tmp); for (auto &c : word_s) cout << c << endl; //不知道为什么运行的时候需要ctrl+z 退出两次 system("pause"); return 0; }
3.18
不合法,因为ivec不包含任何元素,所以ivec[0]错误。
改成 vector<int> ivec(10);
//3.19 当然第一个好! # include<iostream> # include<string> # include<vector> # include<cctype> using namespace std; int main() { vector<int> v1(10, 42); vector<int> v2{ 42,42,42,42,42,42,42,42,42,42 }; vector<int> v3; for (int i = 0; i < 10; i++) v3.push_back(42); cout << "v1:"; for (auto &c : v1) cout << c << " "; cout << endl; cout << "v2:"; for (auto &c : v2) cout << c << " "; cout << endl; cout << "v3:"; for (auto &c : v3) cout << c << " "; cout << endl; system("pause"); return 0; }
1 //3.20 读入一组书存入vector对象,输出相邻的两个数的和 2 # include<iostream> 3 # include<string> 4 # include<vector> 5 # include<cctype> 6 using namespace std; 7 8 int main() 9 { 10 vector<int> v1; 11 cout << "输入一组整数:"; 12 int integer; 13 while (cin >> integer) 14 v1.push_back(integer); 15 16 17 cout << "结果是:"; 18 for(int i = 0; i < v1.size()-1; i++) 19 cout << v1[i] + v1[i + 1] << " "; 20 21 22 system("pause"); 23 24 return 0; 25 }
//3.20改 输出第一个和倒数第一个的和 第二个和倒数第二个的和 以此类推
//就不考虑奇数个值时,中值会*2的特殊情况了
# include<iostream> # include<string> # include<vector> # include<cctype> using namespace std; int main() { vector<int> v1; cout << "输入一组整数:"; int integer; while (cin >> integer) v1.push_back(integer); cout << "结果是:"; for(int i = 0; i < v1.size()-1; i++) cout << v1[i] + v1[v1.size()-i-1] << " "; system("pause"); return 0; }
//3.21 使用迭代器重做94 其实我也不知道指的是哪道题。。可能我的书有问题吧。。。。 # include<iostream> # include<string> # include<vector> # include<cctype> using namespace std; int main() { vector<int> v1; int num[10] = { 1,2,3,4,5,6,7,8,9,10 }; int m = 0; while (m != 10) { v1.push_back(num[m]); m++; } for (auto it = v1.cbegin(); it != v1.cend(); it++) { cout << *it << " "; } system("pause"); return 0; }
1 //3.22 把text全部改为大写 用迭代器 2 # include<iostream> 3 # include<string> 4 # include<vector> 5 # include<cctype> 6 using namespace std; 7 8 int main() 9 { 10 11 string str("wofjsang,test"); 12 for (auto it = str.begin(); it != str.end(); it++) 13 { 14 *it = toupper(*it); 15 cout << *it; 16 } 17 18 19 cout << endl; 20 21 22 23 system("pause"); 24 25 return 0; 26 }
//3.22 把text全部改为大写 用迭代器 # include<iostream> # include<string> # include<vector> # include<cctype> using namespace std; int main() { vector<int> t{ 1,3,4,5,2,7,8,5,6,10 }; for (auto i = t.begin(); i != t.end(); i++) { *i *= *i; } for (auto &c : t) { cout << c << " "; } cout << endl; system("pause"); return 0; }
//3.23 创建含有十个整数的vector对象,然后使用迭代器将所有元素的值都变成原来的两倍。输出vector对象检测是否正确。 # include<iostream> # include<string> # include<vector> # include<cctype> using namespace std; int main() { vector<int> t{ 1,3,4,5,2,7,8,5,6,10 }; for (auto i = t.begin(); i != t.end(); i++) { *i *= *i; } for (auto &c : t) cout << c << " "; cout << endl; system("pause"); return 0; }
//3.24 使用迭代器重做3.20 读入一组数 输出相邻两个和 改输出第一个数,最后一个数的和,第二个和倒数第二个 以此类推 # include<iostream> # include<string> # include<vector> # include<cctype> using namespace std; int main() { vector<int> continer; int tmp; // 书上说用迭代器遍历的时候不能进行增删操作,不过先定义了迭代器,只有循环输入应该没关系。 cout << "输入一组整数:"; while (cin >> tmp) continer.push_back(tmp); cout << "相邻之和:"; for (auto t1 = continer.begin(); t1 < continer.end() - 1; t1++) { cout << *t1 + (*t1+1) << " "; } cout << endl; cout << "首尾相加:"; for (auto t1 = continer.begin(),t2 = continer.end()-1; t1 <= t2; t1++, t2--) { if (t1 == t2) cout << *t1; else cout << *t1 + *t2 << " "; } cout << endl; system("pause"); return 0; }
//3.25 划分分数段 # include<iostream> # include<string> # include<vector> # include<cctype> using namespace std; int main() { vector<int> continer (11); int tmp; // 书上说用迭代器遍历的时候不能进行增删操作,不过先定义了迭代器,只有循环输入应该没关系。 cout << "输入一组分数整数:"; while (cin >> tmp) { auto t = continer.begin(); int a = tmp / 10; *(t + a) += 1; } for (auto t = continer.begin(); t < continer.end(); t++) cout << *t << " "; cout << endl; system("pause"); return 0; }
3.26 因为两个迭代器 beg 和 end 不能直接相加。
3.27
(c) 如果txt_size()是一个constexpr 就是正确的
(d) 错误,空间不够
3.28
sa 为空
ia 为10个为0的int
sa2 数组为空。
ia2 数组含有10个0的int。
3.29
大小确定 不便捷
3.30 ix<= array_size 越界
//3.31 # include<iostream> # include<string> # include<vector> # include<cctype> using namespace std; int main() { int a[10]; for (int i = 0; i < 10; i++) { a[i] = i; cout << i << " "; } cout << endl; system("pause"); return 0; }
//3.32 # include<iostream> # include<string> # include<vector> # include<cctype> using namespace std; int main() { constexpr size_t array_size = 10; vector<int> a(array_size); for (int i = 0; i < array_size; i++) a[i] = i; vector<int> b = a; for (auto &t : b) cout << t << " "; cout << endl; system("pause"); return 0; }
3.33 不初始化的话 这个数组是未定义的, 不能从0开始计数。
3.34 p1指向p2所指的元素 当p1 为尾后指针时错误
//3.35 # include<iostream> # include<string> # include<vector> # include<cctype> using namespace std; int main() { int a[5] = { 1,2,3,4,5 }; int *p = &a[0]; for (int i = 0; i < 5; i++) { *p = 0; p++; } for (int i = 0; i < 5; i++) { cout << a[i] << " "; }
cout << endl; system("pause"); return 0; }
3.36 数组先比长度,再逐个值比较, vector 直接用==比较。
//3.37 因为这个char数组不是以 /0结尾,所以下面while循环的时候会一直向下寻找下去,直到遇到0为止,所以会输出hello后 很多空格 换行符 # include<iostream> # include<string> # include<vector> # include<cctype> using namespace std; int main() { const char ca[] = { 'h', 'e', 'l', 'l', 'o' }; const char* cp = ca; while (*cp) { cout << *cp << endl; ++cp; } cout << endl; system("pause"); return 0; }
3.38 两个指针的地址相加,指向了一个内存地址,可是这个内存地址上没有值,所以没有意义。
//3.39 因为这个char数组不是以 /0结尾,所以下面while循环的时候会一直向下寻找下去,直到遇到0为止,所以会输出hello后 很多空格 换行符 # include<iostream> # include<string> # include<vector> # include<cctype> using namespace std; int main() { char ia[] = "sup"; char iaa[] = "suP"; if (strcmp(ia, iaa) > 0) cout << "ia da" << endl; else if (strcmp(ia, iaa) < 0) cout << "iaa da" << endl; else cout << "dou da" << endl; string ai("sup"); string aii("suP"); if (ai > aii) cout << "ai da"; else if (ai < aii) cout << "aii da"; else cout << "dou da"; system("pause"); return 0; }
//3.40 visual stdio 2015 里面 不让用strcat 和 strcpy函数,报错不安全 ,用strcpy_s,和 strcat_s报错说空间不够,str这个数组不是空的,所以以后C++ 少用C语言的字符串风格。这个例子在dev_c++ 编译通过。
# include<iostream> # include<string> # include<vector> # include<cctype> # include<cstring> using namespace std; int main() { char str1[] = { 'h', 'e','l','l','o' }; char str2[] = { 'a','b','c','\0' }; char str[10] = {}; strcat(str1, str2); strcpy(str, str1); char *p = str; cout << str; system("pause"); return 0; }
//3.41 使用整数数组初始化vector int对象 # include<iostream> # include<string> # include<vector> # include<cctype> # include<cstring> using namespace std; int main() { int a[5] = { 1,2,3,4,5 }; vector<int> t(begin(a), end(a)); for (auto &c : t) cout << c << " "; cout << endl; system("pause"); return 0; }
//3.42 将含有整型元素的vector拷贝给整形数组 # include<iostream> # include<string> # include<vector> # include<cctype> # include<cstring> using namespace std; int main() { vector<int> num = { 1,2,3,4,5 }; int a[6]; for (int i = 0; i < num.size(); i++) { a[i] = num[i]; cout << a[i] << " "; } cout << endl; system("pause"); return 0; }
//3.43 # include<iostream> # include<string> # include<vector> # include<cctype> # include<cstring> using namespace std; int main() { int ia[3][4] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; for ( int(&p)[4] : ia) for (int &q : p) cout << q << " "; cout << endl; for (size_t i = 0; i != 3; i++) for (size_t j = 0; j != 4 ; j++) cout << ia[i][j] << " "; cout << endl; for (int(*p)[4] = ia; p != ia + 3; p++) for (int *q = *p; q != *p + 4; q++) cout << *q << " "; cout << endl; system("pause"); return 0; }
//3.44 #include <iostream> #include <string> using namespace std; int main() { int ia[3][4] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; using int_array = int[4]; for (int_array& p : ia) for (int q : p) cout << q << " "; cout << endl; for (size_t i = 0; i != 3; ++i) for (size_t j = 0; j != 4; ++j) cout << ia[i][j] << " "; cout << endl; for (int_array* p = ia; p != ia + 3; ++p) for (int* q = *p; q != *p + 4; ++q) cout << *q << " "; cout << endl; system("pause"); return 0; }
//3.45 #include <iostream> #include <string> using namespace std; int main() { int ia[3][4] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; using int_array = int[4]; for (auto& p : ia) for (int q : p) cout << q << " "; cout << endl; for (auto i = 0; i != 3; ++i) for (size_t j = 0; j != 4; ++j) cout << ia[i][j] << " "; cout << endl; for (auto * p = ia; p != ia + 3; ++p) for (int* q = *p; q != *p + 4; ++q) cout << *q << " "; cout << endl; system("pause"); return 0; }