essential c++ ch3

发布时间:2019-06-16 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了essential c++ ch3脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

3.1

#include <;map>
#include <set>
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <iterator>

using namespace std;

void initialize_exclusion_set(set<string> &);//初始化排除的序列
void process_file(map<string, int>&, const set<string> &, ifstream&);
void user_query(const map<string, int>&);
void display_word_count(const map<string, int>&, ofstream&);

int main()
{
    ifstream ifile("input.txt");
    ofstream ofile("output.txt");
    if (!ifile || !ofile)
    {
        cerr << "unable to open files!n";
        return -1;
    }

    set<string> exclude_set;
    initialize_exclusion_set(exclude_set);

    map<string, int> word_count;
    process_file(word_count, exclude_set, ifile);
    user_query(word_count);
    display_word_count(word_count, ofile);
}

void initialize_exclusion_set(set<string> &exs)
{
    static string _excluded_words[5] = {"the", "and", "but", "are", "then"};
    exs.insert(_excluded_words, _excluded_words + 5);
}
void process_file(map<string, int>&word_count, const set<string> &exclude_set, ifstream &ifile)
{
    string word;
    while (ifile >> word)
    {
        if (exclude_set.count(word))//判断是否需要排除
            continue;
        word_count[word]++;
    }
}
void user_query(const map<string, int>&word_map)
{
    string search_word;
    cout << "Please enter a word to search: q to quit:";
    cin >> search_word;
    while(search_word.size() && search_word !="q")
    {
        map<string, int>::const_iterator it;
        if ((it = word_map.find(search_word)) != word_map.end())
        {
            cout << "Found  " << it->first   //key
                   <<" occurs " << it->second  //value
                   <<" times.n";
        }
        else
            cout << search_word << " was not found in text.n";
        cout << "nAnother search?(q to quit) ";
        cin >> search_word;
    }
}

void display_word_count(const map<string, int>& word_map, ofstream& os)
{
    map<string, int>::const_iterator iter = word_map.begin(), end_it = word_map.end();
    while(iter  != end_it)
    {
        os << iter->first << " ( "
            << iter->second << " ) " << endl;
        ++iter;
    }
    os << endl;
}

3.2

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

using namespace std;

class LessThan
{
public:
    bool operator()(const string& s1, const string& s2)
    {
        return s1.size() < s2.size();
    }
};

template<typename elemType>
void display_vector(const vector<elemType> &vec, ostream &os = cout, int len = 8)
{
    vector<elemType>::const_iterator iter = vec.begin(), end_it  = vec.end();
    int elem_cnt = 1;
    while (iter != end_it)
    {
        os << *iter++
            <<(!(elem_cnt++ % len) ? 'n' : ' ');
        os << endl;
    }
}

int main()
{
    ifstream infile("input.txt");
    ofstream outfile("output.txt");
    vector<string> word;
    string str;
    if( ! infile)
    {
            cerr << "unable to open the file.n";
            return -1;
    }
    while (infile >> str)
    {
        word.push_back(str);
    }
//     vector<string>::iterator it = word.begin(), end_it = word.end();
//     for ( ; it != end_it; it++)
//     {
//         cout << *it << endl;
//     }
    sort(word.begin(), word.end(), LessThan());//这个sort可以直接这么调用,开始想错了,还打算用嵌套循环实现,这个直接就是function object
    display_vector(word, outfile);
}

脚本宝典总结

以上是脚本宝典为你收集整理的essential c++ ch3全部内容,希望文章能够帮你解决essential c++ ch3所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。