练习3.2:
#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
int main()
{
string s1;
//cin >> s1;//使用此句时,请将下一句注释
std::getline(cin,s1);
cout << s1;
return 0;
}
练习3.3
cin>>是遇到空格(包括换行、制表等)就会停止,getline遇到换行符停止。
练习3.4
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1, s2;
getline(cin, s1);
getline(cin, s2);
if (s1 == s2)
cout << "s1、s2相等" << endl;
else
cout << ((s1 > s2) ? s1 : s2) << endl;
return 0;
}
//修改后的程序
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1, s2;
getline(cin, s1);
getline(cin, s2);
if (s1.size() == s2.size())
cout << "s1、s2长度相等" << endl;
else
cout << ((s1.size() > s2.size()) ? s1 : s2) << endl;
return 0;
}
练习3.5
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1, s2;
getline(cin, s1);
getline(cin, s2);
cout << s1 + s2 << endl;
cout << s1 + " " + s2 << endl;
return 0;
}
练习3.6
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1("abcde");
for (auto& a : s1)
a = 'x';
cout << s1;
return 0;
}
练习3.7
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1("abcde");
for (char a : s1)
a = 'x';
cout << s1;
return 0;
}
s1的内容没有被改变
练习3.8
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1("abcde");
for (int i = 0; i < s1.size(); ++i)
s1[i] = 'x';
cout << s1;
return 0;
}
//下面是另一种代码
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1("abcde");
int i = s1.size();
while (i--)
s1[i] = 'x';
cout << s1;
return 0;
}
练习3.9
答:程序输出一个空字符;程序不合法,因为引用字符串中的元素时,若元素为空,则该行为是未定义的。
练习3.10
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string s;
string s1;
getline(cin, s);
for (auto& a : s)
if (!ispunct(a))
s1+=a;
cout << s1 << endl;
return 0;
}
练习3.11
答:合法,a的类型是const char
练习3.12
答:a、c正确,a的容器中的元素是vector容器,c容器中共10个元素,每个元素都是“null”;b错误,ivec与svec类型不符,无法进行赋值
练习3.13
答:a为空;b包含10个元素每个元素都为0;c包含10个元素,每个元素都为42;d包含一个元素10;e包含两个元素10、42;f有十个默认初始化的元素;g有十个元素,都为“hi”
练习3.14
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> a;
int b;
while (cin >> b)
a.push_back(b);
return 0;
}
练习3.15
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<string> a;
string b;
while (cin >> b)
a.push_back(b);
return 0;
}
//程序按crtl+z+enter结束
练习3.16
这里是第七个的验证,读者可自行更改,验证其余选项
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<string> v7{10,"hi"};
for (auto a : v7)
cout << a << " ";
return 0;
}
练习3.17
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<string> s;
string s1;
while (cin >> s1)
s.push_back(s1);
for (auto s2 : s)
cout << s2 << endl;
return 0;
}
练习3.18
答:不合法,可以将在定义容器时初始化,则下面的语句正确
练习3.19
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> a(10, 42);
vector<int> b{ 42,42,42,42,42,42,42,42,42,42 };
vector<int> c(10);
for (auto& c1 : c)
c1 = 42;
return 0;
}
在元素都相同时,选择第一种更加方便。
练习3.20
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> a;
int b;
while (cin >> b)
a.push_back(b);
for (int i = 0; i < a.size() - 1; ++i)
cout << a[i] + a[i + 1] << endl;
for (int i = 0; i < a.size()/ 2; ++i)
cout << a[i] + a[a.size() - 1 - i] << endl;
return 0;
}
练习3.21
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<string> v7{ 10,"hi" };
for (auto i = v7.begin(); i != v7.end(); ++i)
cout << *i << " ";
return 0;
}
练习3.23
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> a;
for (int i = 1; i <= 10; ++i)
a.push_back(i);
for (auto& b : a)
b *= 2;
for (auto c : a)
cout << c <<" ";
return 0;
}
练习3.24
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> a;
int b;
while (cin >> b)
a.push_back(b);
for (auto i = a.begin(); i !=a.end() - 1; ++i)
cout << *i + *(i+1) << endl;
auto it_begin = a.begin();
auto it_end = a.end()-1;
while (it_begin < it_end)
{
cout << *it_begin + *it_end << endl;
++it_begin;
--it_end;
}
return 0;
}
练习3.25
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<unsigned> scores(11, 0);
unsigned grades;
while (cin >> grades)
{
if (grades <= 100)
++(*(scores.begin() + grades / 10));
}
for (auto a : scores)
cout << a << " ";
return 0;
}
练习3.26
答:因为此时的end还没有更新,所以需要使用(end-beg)/2来更新end
练习3.27
a,c非法,因为数组的大小必须为一个常量;d非法,char数组不能用字符串初始化
练习3.28
sa、sa2为空串;ia、ia2为0;
练习3.29
定义时必须要确定大小。
练习3.30
数组第一个元素应该从0开始
练习3.31
#include <iostream>
using namespace std;
int main()
{
int a[10];
for (int i = 0; i < 10; ++i)
a[i] = i;
return 0;
}
练习3.32
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int a[10];
for (int i = 0; i < 10; ++i)
a[i] = i;
int b[10];
for (int j = 0; j < 10; ++j)
b[j] = a[j];
vector<int> c(10), d(10);
int k = 0;
for (auto& e : c)
{
e = k;
++k;
}
d = c;
return 0;
}
练习3.33
答:无法正确计数
练习3.34
答:将p2移动到p1的位置,当p1和p2不指向同一个数组的元素时,该程序是非法的。
练习3.35
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int a []= { 1,2,3,4,5 };
int* p =a;
int* p_end = end(a);
while (p!=p_end)
{
*p = 0;
p++;
}
return 0;
}
练习3.36
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int a[10];
int b[10];
int a_len = sizeof(a) / sizeof(a[0]);
int b_len = sizeof(b) / sizeof(b[0]);
if (a_len != b_len)
cout << "a与b不相等" << endl;
else
{
int count = 0;
for (int i = 0; i < a_len; ++i)
{
if (a[i] != b[i])
{
cout << "a与b不相等" << endl;
break;
}
else
count++;
}
if (count == a_len)
cout << "a与b相等" << endl;
}
vector<int> a1(10);
vector<int> b1(10);
if (a1 == b1)
cout << "a1与b1相等" << endl;
else
cout << "a1与b1不相等" << endl;
return 0;
}
练习3.37
答:将数组ca中的元素输出
练习3.38
答:指针是地址,两个地址相加无意义
练习3.39
此题代码与3.36类似,读者可参考
练习3.40
练习3.41/练习3.42
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> a;
int a1[] = { 1,2,3,4,5 };
for (int i = 0; i < sizeof(a1) / sizeof(a[0]); ++i)
a.push_back(a1[i]);
vector<int> b(10, 10);
int b2[10];
for (int i = 0; i < 10; ++i)
b2[i] = b[i];
return 0;
}
练习3.43
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int ia[] = { 1,2,3,4,5 };
for (int a : ia)
cout << a << " ";
for (int i = 0; i < sizeof(ia) / sizeof(ia[0]); ++i)
cout << ia[i] << " ";
for (int* p = ia; p != end(ia); p++)
cout << *p << " ";
return 0;
}
练习3.44
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int ia[] = { 1, 2, 3, 4, 5 };
// Using auto for type deduction
for (auto a : ia)
cout << a << " ";
cout << endl;
// Using auto& to avoid copying
for (auto& a : ia)
cout << a << " ";
cout << endl;
// Using typedef for type aliasing
typedef int* IntPointer;
for (IntPointer p = begin(ia); p != end(ia); ++p)
cout << *p << " ";
return 0;
}
练习3.45
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int ia[] = { 1,2,3,4,5 };
for (auto a : ia)
cout << a << " ";
for (auto i = 0; i < sizeof(ia) / sizeof(ia[0]); ++i)
cout << ia[i] << " ";
for (auto p = ia; p != end(ia); p++)
cout << *p << " ";
return 0;
}