练习3.21
请使用迭代器重做3.3.3节的第一个练习。
解答:
这个就略过吧。
练习3.22
修改之前那个输出text第一段程序,首先把text的第一段全部都改成大写形式,然后再输出它。
解答:
transform(text.begin(), text.end(), text.begin(), toupper);
for(auto it = text.cbegin(); it != text.cend() && !it->empty(); ++it)
cout << *it << endl;
练习3.23
编写一段程序,创建一个含有10个整数的vector对象,然后使用迭代器将所有元素的值都变成原来的两倍。输出vector对象的内容,检验程序是否正确。
解答:
#include <iostream>
#include <vector>
using namespace std;
int main(){
int num[] = { 1, 3, 4, 43, 4, 354, 5, 3, 2, 4, };
vector<int> ivec(num, num + 10);
for (auto it = ivec.begin(); it != ivec.end(); ++it){
*it *= 2;
}
for (auto i : ivec){
cout << i << endl;
}
}