C++Primer第五版课后习题答案目录
3.2
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
//3.2a
while (getline(cin, s))
cout << s << endl;
//3.2b
/*while (cin >> s)
cout << s << endl;*/
return 0;
}
3.4
#include <iostream>
using namespace std;
int main()
{
//3.4a
/*string s1, s2;
cout << "Please enter two strings:" << endl;
while(cin >> s1 >> s2)
{
if (s1 == s2)
cout << "s1 == s2" << endl;
else if (s1 > s2)
cout << "The bigger string is:" << s1 << endl;
else
cout << "The bigger string is:" << s2 << endl;
cout << endl;
cout << "Please enter two strings:" << endl;
}*/
//3.4b
string s1, s2;
cout << "Please enter two strings:" << endl;
while(cin >> s1 >> s2)
{
if (s1.size() == s2.size())
cout << "s1.size() == s2.size()" << endl;
else if (s1.size() > s2.size())
cout << "The longer string is:" << s1 << endl;
else
cout << "The longer string is:" << s2 << endl;
cout << endl;
cout << "Please enter two strings:" << endl;
}
return 0;
}
3.5
#include <iostream>
#include <string>
using namespace std;
int main()
{
//3.5a
/*string buffer, concatenated;
while (cin >> buffer)
concatenated += buffer;
cout << "The concatenated string is:" << concatenated << endl;*/
//3.5b
string buffer, concatenated;
while (getline(cin, buffer))
concatenated += buffer;
cout << "The concatenated string is:" << concatenated << endl;
return 0;
}
3.6
#include <iostream>
using namespace std;
int main()
{
string s(5, 'c');
for (char& c : s)
c = 'X';
cout << s << endl;
return 0;
}
3.20
#include <vector>
using namespace std;
int main()
{
vector<int> ivec;
for (int val; cin >> val; ivec.push_back(val));
if (ivec.empty())
{
cout << "input at least one integer." << endl;
return -1;
}
if (ivec.size() == 1)
{
cout << "only one integer:" << ivec[0] << endl;
return -1;
}
//3.20a
/*for (int i = 0; i < ivec.size() - 1; ++i)
{
cout << ivec[i] + ivec[i + 1] << " ";
}
cout << endl;*/
//3.20b
for (int i = 0, j = ivec.size() - 1; i <= j; ++i, --j)
{
cout << ivec[i] + ivec[j] << " ";
}
cout << endl;
return 0;
}
3.22
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> v;
string words;
/*while (getline(cin, words))
{
v.push_back(words);
}
for (auto it = v.begin(); it != v.end(); ++it)
{
for (auto& c : *it)
{
c = toupper(c);
}
cout << *it << endl;
}*/
while (getline(cin, words))
v.push_back(words);
for (auto& s : v)
{
for (auto& c : s)
if (isalpha(c))
c = toupper(c);
cout << s << endl;
}
return 0;
}
3.23
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v;
for (int i = 0; i != 10; ++i)
{
v.push_back(i);
}
for (auto it = v.begin(); it != v.end(); ++it)
{
(*it) *= 2;
}
for (auto value : v)
{
cout << value << endl;
}
return 0;
}
3.24
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> ivec;
for (int val; cin >> val; ivec.push_back(val));
if (ivec.empty())
{
cout << "input at least one integer" << endl;
return -1;
}
if (ivec.size() == 1)
{
cout << "only one integer:" << ivec[0] << endl;
return -1;
}
//3.24a
/*for (auto it = ivec.begin(); it != (ivec.end() - 1); ++it)
{
cout << (*it) + *(it + 1) << " ";
}
cout << endl;*/
//3.24b
for (auto left = ivec.begin(), right = ivec.end() - 1; left <= right; ++left, --right)
{
cout << (*left) + (*right) << " ";
}
cout << endl;
return 0;
}
3.31
#include <iostream>
using namespace std;
int main()
{
int arr[10];
for (int i = 0; i < 10; i++)
{
arr[i] = i;
}
for (int i = 0; i < 10; i++)
{
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
3.32
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int arr[10];
for (int i = 0; i < 10; i++)
{
arr[i] = i;
}
int arr2[10];
for (int i = 0; i < 10; i++)
{
arr2[i] = arr[i];
}
for (int i = 0; i < 10; i++)
{
cout << arr2[i] << " ";
}
cout << endl;
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
vector<int> v2 = v;
for (int i = 0; i < 10; i++)
{
cout << v2[i] << " ";
}
return 0;
}
3.35
#include <iostream>
using namespace std;
int main()
{
int arr[2] = { 1, 2 };
int* p = arr;
p[0] = 0;
p[1] = 0;
for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
{
cout << arr[i] << " ";
}
return 0;
}
3.36
#include <iostream>
#include <vector>
using namespace std;
bool compare(int* begin1, int* end1, int* begin2, int* end2)
{
if ((end1 - begin1) != (end2 - begin2)) //两数组长度不等
return false;
else
{
for (int i = 0; i < (end1 - begin1); i++)
{
if (begin1[i] != begin2[i])
{
return false;
}
}
return true;
}
}
int main()
{
int arr[5] = { 0, 1, 2, 3, 4 };
int arr2[5] = { 0, 1, 2, 3, 4 };
if (compare(begin(arr), end(arr), begin(arr2), end(arr2)))
cout << "arr == arr2" << endl;
else
cout << "arr != arr2" << endl;
vector<int> v1 = { 0, 1, 2 };
vector<int> v2 = { 1, 2 };
if (v1 == v2)
cout << "v1 == v2" << endl;
else
cout << "v1 != v2" << endl;
return 0;
}
3.39
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1 = "string";
string s2 = "string";
if (s1 == s2)
cout << "s1 == s2" << endl;
else
cout << "s1 != s2" << endl;
char cString1[] = "cstring";
const char* cString2 = "cstring";
if (strcmp(cString1, cString2) == 0)
cout << "cstring1 == cstring2" << endl;
else
cout << "cstring1 != cstring2" << endl;
return 0;
}
3.40
#include <iostream>
#include <cstring>
using namespace std;
const char cstr1[] = "string1";
const char cstr2[] = "string2";
int main()
{
const int newSize = sizeof(cstr1) + sizeof(cstr2) - 1;
char cstr3[newSize] = { 0 };
strcat(cstr3, cstr1);
strcat(cstr3, cstr2);
cout << cstr3 << endl;
return 0;
}
3.41
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int arr[] = { 0, 1, 2, 3, 4, 5 };
vector<int> ivec(begin(arr), end(arr));
for (int i = 0; i < ivec.size(); i++)
{
cout << ivec[i] << " ";
}
cout << endl;
return 0;
}
3.42
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> ivec = { 0, 1, 2, 3, 4, 5 };
int arr[6];
for (int i = 0; i < ivec.size(); i++)
{
arr[i] = ivec[i];
}
for (auto i : arr)
{
i += 1;
cout << i << " ";
}
cout << endl;
return 0;
}
对于P115中int (*p) [4] = ia;为什么不是int (*p) [3] = ia;的思考
因为数组名是指向数组首元素的指针,所以ia是一个含有4个整数的数组的地址,即&ia[0]
#include <iostream>
using namespace std;
int main()
{
int ia[3][4] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
int arr[4] = { 0, 1, 2, 3 };
int(*p)[4] = ia;
int(*p2)[4] = &arr;
int* p3 = arr;
int(*p4)[3][4] = &ia;
cout << "p:" << (int)p << endl;
cout << "p + 1:" << (int)(p + 1) << endl;
cout << "ia:" << (int)ia << endl;
cout << "(ia+1):" << (int)(ia + 1) << endl;
cout << "ia[0]:" << (int)ia[0] << endl;
cout << "ia[0] + 1:" << (int)(ia[0] + 1) << endl;
cout << "p4:" << (int)(p4) << endl;
cout << "p4 + 1:" << (int)(p4 + 1) << endl;
cout << "p:" << typeid(p).name() << endl;
cout << "p[0]:" << typeid(p[0]).name() << endl;
cout << "p[0][0]:" << typeid(p[0][0]).name() << endl;
cout << "p2:" << typeid(p2).name() << endl;
cout << "ia:" << typeid(ia).name() << endl;
cout << p3[1] << endl;
return 0;
}
3.43
#include <iostream>
using namespace std;
int main()
{
//3行4列数组
int ia[3][4] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
//1
//raw为 对含有4个int的数组的引用
/*for (int (&raw)[4] : ia)
{
for (int column : raw)
{
cout << column << " ";
}
cout << endl;
}*/
//2
/*for (int raw = 0; raw < sizeof(ia) / sizeof(ia[0]); raw++)
{
for (int column = 0; column < sizeof(ia[0]) / sizeof(ia[0][0]); column++)
{
cout << ia[raw][column] << " ";
}
cout << endl;
}*/
//3
//raw为 含有4个int的数组的指针
for (int(*raw)[4] = begin(ia); raw != end(ia); raw++)
{
//column为 含有4个int的数组的首地址
for (int* column = begin(*raw); column != end(*raw); column++)
{
cout << *column << " ";
}
cout << endl;
}
return 0;
}
3.44
#include <iostream>
using namespace std;
typedef int intArray[4];
int main()
{
int ia[3][4] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
//1
//raw为 对含有4个int的数组的引用
/*for (intArray &raw : ia)
{
for (int column : raw)
{
cout << column << " ";
}
cout << endl;
}*/
//2
/*for (int raw = 0; raw < sizeof(ia) / sizeof(ia[0]); raw++)
{
for (int column = 0; column < sizeof(ia[0]) / sizeof(ia[0][0]); column++)
{
cout << ia[raw][column] << " ";
}
cout << endl;
}*/
//3
//raw为 含有4个int的数组的指针
for (intArray* raw = ia; raw != end(ia); raw++)
{
for (int* column = begin(*raw); column != end(*raw); column++)
{
cout << *column << " ";
}
cout << endl;
}
return 0;
}
3.45
#include <iostream>
using namespace std;
int main()
{
int ia[3][4] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
//1
/*for (auto& raw : ia)
{
for (auto column : raw)
{
cout << column << " ";
}
cout << endl;
}*/
//2
/*for (auto i = 0; i < sizeof(ia) / sizeof(ia[0]); i++)
{
for (auto j = 0; j < sizeof(ia[0]) / sizeof(ia[0][0]); j++)
{
cout << ia[i][j] << " ";
}
cout << endl;
}*/
//3
for (auto raw = begin(ia); raw != end(ia); raw++)
{
for (auto column = begin(*raw); column != end(*raw); column++)
{
cout << *column << " ";
}
cout << endl;
}
return 0;
}