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);
}

本文固定链接: http://www.js-code.com/cpp/cpp_61401.html