练习3.1

#include<iostream>#include<fstream>#include<iterator>#include<vector>#include<set>#include<map>int main() {    std::ifstream in_file("news.txt");    std::ofstream out_file("news_word_cnt.txt");    if (!in_file || !out_file) {        std::cout << "can not read/write file" << std::endl;    }    // 新建一个vector 而后应用泛型算法copy将元素填充到其中    std::vector<std::string> text;    std::string useless_words[3] = {"a", "and", "is"};    std::set<std::string> useless_set(useless_words, useless_words + 3);    std::map<std::string, int> words_cnt;    // 应用iostream iterator    std::istream_iterator<std::string> in_stream(in_file); // 代表in_file的begin    std::istream_iterator<std::string> in_eof; // 代表in_file的end    std::copy(in_stream, in_eof, std::back_inserter(text)); // 前两个的iterator复制到前面的iterator    for (std::string word: text) {        if (useless_set.count(word) > 0) {            continue;        }        if (words_cnt.count(word)  == 0 ) {            words_cnt[word] = 1;        } else {            words_cnt[word]++;        }    }    // map加工为字符串输入    std::vector<std::string> map_str;    auto it = words_cnt.begin();    while (it != words_cnt.end()) {        map_str.push_back("" + (it -> first) + " : " + std::to_string(it -> second));        it++;    }    std::ostream_iterator<std::string> out_stream(out_file, "\n"); // 分隔符为空格    std::copy(map_str.begin(), map_str.end(), out_stream); // 为什么这里能够间接写out_stream 呢?    return 0;}

练习3.2

#include<iostream>#include<fstream>#include<iterator>#include<vector>#include<set>#include<map>#include<algorithm>bool comp_str(std::string str1, std::string str2) {    return str1.size() > str2.size();}int main() {    std::ifstream in_file("news.txt");    if (!in_file) {        std::cout << "can not read/write file" << std::endl;    }    // 新建一个vector 而后应用泛型算法copy将元素填充到其中    std::vector<std::string> text;    std::string useless_words[3] = {"a", "and", "is"};    std::set<std::string> useless_set(useless_words, useless_words + 3);    std::map<std::string, int> words_cnt;    // 应用iostream iterator    std::istream_iterator<std::string> in_stream(in_file); // 代表in_file的begin    std::istream_iterator<std::string> in_eof; // 代表in_file的end    std::copy(in_stream, in_eof, std::back_inserter(text)); // 前两个的iterator复制到前面的iterator    for (std::string word: text) {        if (useless_set.count(word) > 0) {            continue;        }        if (words_cnt.count(word)  == 0 ) {            words_cnt[word] = 1;        } else {            words_cnt[word]++;        }    }    // 对text依照字符串长度排序    std::sort(text.begin(), text.end(), comp_str);    for (std::string word: text) {        std::cout << word << std::endl;    }    return 0;}

练习3.3

#include<iostream>#include<vector>#include<string> #include<map>// 这里map的> 和 vector的> 两头要有空格void printFamily(const std::string &first_name, const std::map<std::string, std::vector<std::string>* >* family_map) {    if ((*family_map).count(first_name) > 0) {        // 这里find拿到的是一个pair, 取 pair -> second 才是 value        auto names = family_map -> find(first_name);        for (std::string name : *(names -> second)) {            std::cout << "first name:" << name << std::endl;        }    }}int main() {    // 这里定义value为一个指向vector的指针, 避免复制    std::map<std::string, std::vector<std::string>* > family_map;    std::string family_1 = "Last Name";    std::string last_name_arr[6] = {"last name1", "last name2", "last name3", "last name4", "last name5", "last name6"};    std::vector<std::string> last_name_vec(last_name_arr, last_name_arr + 6);    family_map[family_1] = &last_name_vec;    printFamily(family_1, &family_map);    return 0;}

练习3.4

#include<iostream>#include<iterator>#include<algorithm>#include<fstream>#include<vector>int main() {    // 定义输出流    std::istream_iterator<int> is(std::cin);    // 出入eof完结    std::istream_iterator<int> eof;    std::vector<int> numbers;    std::copy(is, eof, std::back_inserter(numbers));    std::vector<int> odd_numers;    std::vector<int> even_numbers;    for (int number : numbers) {        if (number % 2 == 0) {            even_numbers.push_back(number);        } else {            odd_numers.push_back(number);        }    }        // 分两个流写出文件    // 留神这里是ofstream 不是 ostream ...    // 共有三层关系    // 1. 定义一个 out-file-stream , 往哪里写    std::ofstream os_odd("odd_numbers.file");    std::ofstream os_even("even_numbers.file");    // 2. 定义一个 ostream_iterator , 怎么写    std::ostream_iterator<int> os_odd_iter(os_odd, " ");    std::ostream_iterator<int> os_even_iter(os_even, " ");    // 3. 定义一个 copy, 用什么数据写    std::copy(odd_numers.begin(), odd_numers.end(), os_odd_iter);    std::copy(even_numbers.begin(), even_numbers.end(), os_even_iter);    return 0;}

这一章课后题还是比较简单, 然而我感觉有些货色还是须要花肯定的工夫去了解的, 次要就是指"如何设计一个泛型算法"那里, 一步一步的由一个一般的定制化的函数, 通过引入函数指针, find_if()泛型算法, function object, function object adapter 来设计成为一个元素无关, 比拟操作符无关, 容器类型无关的泛型算法函数, 值得重复浏览。