《Primer C++》第五版习题:第三章

3.1

这个题就是让你加个using namespace std;这样就不用写很多std::了

3.2

int main(){
    string str;
    while(getline(cin,str));//这是读入一行
    //while(cin>>str);    这个是读入一个单词
    return 0;
}

3.3

cin遇到空白字符会停止读取,空白字符还在缓冲区。

getiline会读入空白字符,但是最后会把它丢弃,不会存到string里去。

3.4

int main()
{
	string s1, s2;
	cin >> s1 >> s2;
	bool result = s1 == s2;
	if (result) { cout << "相等"; }
	else { cout << max(s1, s2); }
	return 0;
}
int main()
{
	string s1, s2;
	cin >> s1 >> s2;
	bool result = s1.length() == s2.length();
	if (result) { cout << "等长"; }
	else { cout << (s1.length()>s2.length()?s1:s2); }//用了三目运算符
	return 0;
}

3.5

int main()
{
	string s,catstring="";
	while (cin>>s)
	{
		catstring += s;
        //catstring+=" ";  多个空格分割
	}
	return 0;
}

3.6

int main()
{
	string str = "abcdefg";
	for (auto& c : str) {//一定要写成引用
		c = 'X';
	}
}

3.7

这个问题感觉没说明白。

如果写成for(char &c:str)的话,结果是一样的;不过不加&只是char c,也就不是引用的话就不对了,这样的话str是没有变化的。

3.8

int main()
{
	string str = "abcdef";
	int i = 0;
	while (i != str.size()) {
		str[i]='X';
		i++;
	}
}
int main()
{
	string str = "abcdef";
	for (int i = 0; i < str.size(); i++)
		str[i] = 'X';
}

其实都差不多了 

3.9

虽然不会报错,但是通常会导致程序崩溃或产生不可预测的结果。尽量不要这样做

3.10

int main()
{
	string str;
	getline(cin, str);
	for (auto c : str)
		if (!ispunct(c))
			cout << c;
}

3.11

不合法,因为auto不会保留顶层const,如果在for循环里改变c会报错。

3.12

(a)正确,是一个空的容器

(b)错误,svec与用来初始化的ivec类型不同

(c)正确,是一个字符串的容器,里面有10个元素,都是"null"

3.13

(a) 0

(b) 10个0

(c) 10个42

(d) 1个10

(e) 2个  10和42

(f) 10个空字符串

(g) 10个"hi"字符串

3.14

int main()
{
	vector<int> vec;
	int i;
	while (cin >> i) {
		vec.push_back(i);
	}
	return 0;
}

3.15

int main()
{
	vector<string> vec;
	string str;
	while (getline(cin,str)) {
		vec.push_back(str);
	}
	return 0;
}

3.16

我就不一个一个容器的输出了,写一个函数吧,把容器传过来然后就输出

void put(const vector<int>& vec) {//当然,这只能输出int容器
	for (const auto& item : vec)
		cout << item;
}

3.17

int main()
{
	vector<string> vec;
	string str;
	while (cin >> str)
		vec.push_back(str);
	for (auto& s : vec) {
		for (auto& c : s) {
			c = toupper(c);//通过这个函数改变每个字符的大小,定义在cctype头文件里
		}
	}
	for (const auto& s : vec)//改完然后输出
		cout << s << endl;
}

3.18

不合法,ivec是一个空的容器,还没有第一个元素

应该改成

vector<int> ivec;
ivec.push_back(42);

3.19

int main()
{
	vector<int> vec1(10, 42);

	vector<int> vec2(10);
	for (int i = 0; i < 10; i++)
		vec2[i] = 10;

	vector<int> vec3;
	for (int i = 0; i < 10; i++)
		vec3.push_back(10);
}

第一种方式更好,它是构造的时候就定好了。其他两种情况容器一开始都是默认初始化的情况。

3.20

int main()
{
	vector<int> vec;
	int x;
	while (cin >> x)
		vec.push_back(x);
	int n = vec.size();
	for (int i = 0; i < n - 1; i++)
		cout << vec[i] + vec[i + 1]<<endl;
}
int main()
{
	vector<int> vec;
	int x;
	while (cin >> x)
		vec.push_back(x);
	int n = vec.size();
	for (int i = 0,j=n-1; i<=j; i++,j--)
		cout << vec[i] + vec[j]<<endl;
}

3.21

同样也是写个函数吧

void put(const vector<int>& vec) {
	for (auto it = vec.cbegin(); it != vec.cend(); it++)
		cout << *it;
}

3.22

int main()
{
	vector<string> vec;
	string str;
	while (cin >> str)
		vec.push_back(str);
	for (auto& s : vec) {
		for (auto it = s.begin(); it != s.end();it++) {//其实就是范围for改成迭代器遍历
			*it = toupper(*it);//通过这个函数改变每个字符的大小,定义在cctype头文件里
		}
	}
	for (const auto& s : vec)//改完然后输出
		cout << s << endl;
}

3.23

int main()
{
	vector<int> vec(10, 5);
	for (auto it = vec.begin(); it != vec.end(); it++) {
		*it *= 2;
		cout << *it;
	}
}

3.24

int main()
{
	vector<int> vec;
	int x;
	while (cin >> x)
		vec.push_back(x);
	int n = vec.size();
	for (auto it = vec.begin(); it != vec.end() - 1; it++)
		cout << *it + *(it + 1) << endl;
}
int main()
{
	vector<int> vec;
	int x;
	while (cin >> x)
		vec.push_back(x);
	int n = vec.size();
	for (auto it = vec.begin(),it2=vec.end()-1; it<=it2; it++,it2--)
		cout << *it + *it2 << endl;
}

3.25

	vector<unsigned> scores(11, 0);
	unsigned grade;
	while (cin >> grade) {
		if (grade <= 100)
			(*(scores.begin() + grade / 10))++;//要再套一层括号,因为++优先级高于*,得先用*取得对应的引用
	}

3.26

begin+end可能溢出没有意义。

后面的内容都是C里面的数组了,我的评价是略过

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值