After class questions of Chapter 3 of essential C + +

Time:2020-10-29

Exercise 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;
    }
    //Create a new vector and use the generic algorithm copy to fill in the vector
    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;
    //Using iostream iterator
    std::istream_ iterator<std::string> in_ stream(in_ File); // stands for in_ Begin of file
    std::istream_ iterator<std::string> in_ EOF; // stands for in_ End of file
    std::copy(in_ stream, in_ eof, std::back_ Inserter (text)); // copy the first two iterators to the later iterators
    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 is processed as string output
    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," "; // the separator is a space
    std::copy(map_ str.begin (), map_ str.end (), out_ Stream); // why can I write out directly here_ What about stream?
    return 0;
}

Exercise 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;
    }
    //Create a new vector and use the generic algorithm copy to fill in the vector
    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;
    //Using iostream iterator
    std::istream_ iterator<std::string> in_ stream(in_ File); // stands for in_ Begin of file
    std::istream_ iterator<std::string> in_ EOF; // stands for in_ End of file
    std::copy(in_ stream, in_ eof, std::back_ Inserter (text)); // copy the first two iterators to the later iterators
    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]++;
        }
    }
    //Sort text by string length
    std::sort(text.begin(), text.end(), comp_str);
    for (std::string word: text) {
        std::cout << word << std::endl;
    }
    return 0;
}

Exercise 3.3

#include<iostream>
#include<vector>
#include<string> 
#include<map>

//There should be a space between the > of map and the > of 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) {
        //Here, find gets a pair. Take pair > second as value
        auto names = family_map -> find(first_name);
        for (std::string name : *(names -> second)) {
            std::cout << "first name:" << name << std::endl;
        }
    }
}

int main() {
    //Here, value is defined as a pointer to vector to prevent duplication
    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;
}

Exercise 3.4

#include<iostream>
#include<iterator>
#include<algorithm>
#include<fstream>
#include<vector>

int main() {
    //Define input flow
    std::istream_iterator<int> is(std::cin);
    //End of 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);
        }
    }
    
    //Write the file in two streams
    //Notice that this is OFSTREAM, not ostream
    //There are three levels of relationship
    //1. Define an out file stream and where to write
    std::ofstream os_odd("odd_numbers.file");
    std::ofstream os_even("even_numbers.file");
    //2. Define an ostream_ Iterator, how to write it
    std::ostream_iterator<int> os_odd_iter(os_odd, " ");
    std::ostream_iterator<int> os_even_iter(os_even, " ");
    //3. Define a copy and write it with what data
    std::copy(odd_numers.begin(), odd_numers.end(), os_odd_iter);
    std::copy(even_numbers.begin(), even_numbers.end(), os_even_iter);
    return 0;
}

The question after class in this chapter is relatively simple, but I think some things still need to take some time to understand. It mainly refers to “how to design a generic algorithm”. Step by step, a common customized function is introducedFunction pointer, find_ If() generic algorithm, function object, function object adapterTo design into aElement independent, Comparison operator independent, Container type independentIt is worth reading again and again.