C++Primer第五版:练习3.22 3.23 3.24 3.25 3.26

练习3.22

int main()
{
    
    
	string text = "Hello \nworld!";

	for (auto it = text.begin(); it != text.end() && *it != '\n'; ++it)
		*it = toupper(*it);

	cout << text << endl;
}

练习3.23

int main()
{
    
    
	vector<int> vec(10, 2);

	for (vector<int>::iterator it = vec.begin(); it != vec.end(); ++it)
		*it *= *it;
}

练习3.24

#include<iostream>
#include<vector>
using namespace std;

int main()
{
    
    
	vector<int> vec;
	int n;

	while (cin >> n)
		vec.push_back(n);
	//first
	for (vector<int>::iterator it = vec.begin(); it != vec.end() - 1; ++it)
		cout << *it + *(it + 1) << ' ';
	cout << endl;

	//modified
	auto beg = vec.begin(), end = vec.end() - 1;
	while (beg < end)
	{
    
    
		cout << *beg + *end << ' ';
		++beg;
		--end;
		if (beg == end)
			cout << *beg << ' ';
	}
	
}

练习3.25

#include<iostream>
#include<vector>
using namespace std;

int main()
{
    
    
	vector<unsigned> scores(11, 0);
	unsigned grade;

	while (cin >> grade)
	{
    
    
		if (grade <= 100)
		{
    
    
			auto it = scores.begin();
			it += grade / 10;
			++* it;
		}
	}
}

练习3.26
end-beg 返回difference_type类型,返回数值表示容器的长度
end+beg 迭代器相加超出范围

猜你喜欢

转载自blog.csdn.net/Xgggcalled/article/details/109124070
今日推荐