C++标准模板库编程实践练习题
第一章
代码仅供参考
practice1
编写一个程序,它定义了一个标准的数组:用你选择的一些词去初始化字符串对象,然后用迭代器一个一行地列出数组的内容
#include<iostream>
#include<string>
#include<iterator>
int main()
{
std::string data[]{"i","am","not","happy"};
std::cout << "The words in data are:" << std::endl;
for (auto iter = std::begin(data); iter != std::end(data); ++iter)
{
std::cout << *iter << " ";
}
}
###practice2
*写一个程序,运用transform()算法,将前一个练习中数组的所有小写元音字母以‘*’ 替换,并将结果一个一行地写入标准输出流。以lambda表达式的方式定义一个使用迭代器的函数来替换字符串中的元音字母*
#include<iostream>
#include<string>
#include<iterator>
#include<algorithm>
int main()
{
std::string data[]{"I", "am", "depressed", "But", "I", "have", "to", "encourange", "Myself"};
for (auto iter = std::begin(data); iter != std::end(data); ++iter)
{
std::transform(std::begin(*iter), std::end(*iter),
std::ostream_iterator<char>(std::cout),
[](char iter1){return ((iter1 == 'a') | (iter1 == 'e') | (iter1 == 'i') |( iter1 == 'o' )| (iter1 == 'u')) ? '*' : iter1; });
std::cout << std::endl;
}
}