#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; }
#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; }
#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; }
#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
來設計成爲一個元素無關
, 比較操做符無關
, 容器類型無關
的泛型算法函數, 值得反覆閱讀。ios