栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

3、泛型编程风格-C++

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

3、泛型编程风格-C++







答案代码:
#include
#include
#include
#include
#include 
#include 
#include 
#include


using namespace std;
typedef vector vstring;
//map families;

void initalize_exclusion_set(set&);
void process_file(map&,const set&,ifstream&);
void user_query(const map&);
void display_word_count(const map&,ofstream&);

void initalize_exclusion_set(set &exs){
    static string _excluded_words[25]={
        "the","and","but","that","then","are","been",
        "can","a","could","did","for","of",
        "had","have","him","his","her","its","is",
        "were","which","when","with","would"
    };
    exs.insert(_excluded_words,_excluded_words+25);
}

void process_file(map& word_count,
                  const set& exclude_set,ifstream& ifile){
    string word;
    while (ifile>>word) {
        if(exclude_set.count(word)){
            continue;
        }
        word_count[word]++;
    }
}

void user_query(const map& 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::const_iterator it;
        if((it=word_map.find(search_word))!=word_map.end())
            cout<<"found! "<first
                <<" occurs "<second
                <<" times.n";
        else {
            cout<>search_word;
    }

}

void display_word_count(const map& word_map,ofstream &os){
    map::const_iterator
            iter=word_map.begin(),
            end_it =word_map.end();
    while (iter!=end_it) {
        os<first<<"("
                      <second<<")"<
    ifstream ifile("G:\QT_project"
                   "\EssentialCPP\build-chap03-Desktop_Qt_5_9_8_MSVC2015_64bit-Debug\column.txt");
    ofstream ofile("G:\QT_project"
                   "\EssentialCPP\build-chap03-Desktop_Qt_5_9_8_MSVC2015_64bit-Debug\column.map");
    if(!ifile||!ofile){
        cerr<<"unable to open file -- bailing out!n";
        return -1;
    }
    set exclude_set;
    initalize_exclusion_set(exclude_set);

    map word_count;
    process_file(word_count,exclude_set,ifile);
    user_query(word_count);
    display_word_count(word_count,ofile);
}

class LessThan{
public:
    bool operator()(const string &s1,const string &s2){
        return s1.size()
void display_vector(const vector&vec,ostream &os=cout,int len=8){
    //vector::cons
    std::vector::const_iterator iter=vec.begin(),end_it=vec.end();
    int elem_cnt=1;
    while (iter!=end_it) {
        os<<*iter++<<(!(elem_cnt++%len)?'n':' ');

    }
    os<
    ifstream ifile("G:\QT_project"
                   "\EssentialCPP\build-chap03-Desktop_Qt_5_9_8_MSVC2015_64bit-Debug\column.txt");
    ofstream ofile("G:\QT_project"
                   "\EssentialCPP\build-chap03-Desktop_Qt_5_9_8_MSVC2015_64bit-Debug\column.map");
    if(!ifile||!ofile){
        cerr<<"unable to open file -- bailing out!n";
        return -1;
    }
    vector text;
    string word;
    while (ifile>>word) {
        text.push_back(word);
    }
    sort(text.begin(),text.end(),LessThan());
    display_vector(text,ofile);

}

//ifstream -- 从已有的文件读
//ofstream -- 向文件写内容
//fstream  -- 打开文件供读写
//getline()是标准库函数,它从文件读取一行内容,其第三个参数用来指定行末字符。默认的行末字符为换行符,
void populate_map(ifstream &nameFile,map &families)
{
    string textline;
    while (getline(nameFile,textline)) {
        string fam_name;
        vector child;
        string::size_type pos =0,prev_pos=0,text_size=textline.size();
        //ok :找出以空格分隔开来的所有单字
        while ((pos=textline.find_first_of(' ',pos))!=string::npos) {
            //计算子字符串的终点
            string::size_type end_pos=pos-prev_pos;
            //倘若prev_pos并未设置(或说其值为0,),那么读到的单字就是家庭姓氏,否则我们就————读取孩子们的名字了
            if(!prev_pos)
                fam_name=textline.substr(prev_pos,end_pos);
            else {
                child.push_back(textline.substr(prev_pos,end_pos));
            }
            prev_pos=++pos;
        }
        //选择处理最后一个孩子的名字
        if(prev_pos
            cerr<<"Oops! We already have a"< &familes,ostream &os){
    map::const_iterator it=familes.begin(),end_it=familes.end();

    while (it!=end_it) {
        os<<" The "<first<<" family ";
        if(it->second.empty())
            os<<" has no childrenn";
        else {
            //打印出vector 内的小孩子名字
            os<< " has "<second.size()<<" children: ";
            vector::const_iterator
                    iter=it->second.begin(),
                    end_iter= it->second.end();
            while (iter!=end_iter) {
                os<<*iter<< "  ";
                ++iter;
            }
            os< &families){

    map::const_iterator
            it=families.find(family);
    if(it==families.end()){
        cout<<"sorry .the "<second.size())
        cout<<" has no childrenn";
    else {//打印所有小孩子的名字
        cout<<" has "<second.size()<<" children: ";
        vector::const_iterator
                iter=it->second.begin(),
                end_iter=it->second.end();
        while (iter!=end_iter) {
            cout<<*iter<< " ";
            ++iter;
        }
        cout<
    map families;
    ifstream nameFile("G:\QT_project"
                      "\EssentialCPP\build-chap03-Desktop_Qt_5_9_8_MSVC2015_64bit-Debug\families.txt");

    if(!nameFile){
        cerr<<"unable to find families.txt file . bailing out !n";
        return  -1;
    }
    populate_map(nameFile,families);
    string family_name;
    while (1) {//除非用户表示要离开,否则一直执行下去
        cout<<"please enter a family name or q to quit";
        cin>>family_name;
        if(family_name=="q")
            break;
        query_map(family_name,families);

    }
    display_map(families,cout);
}



//class even_elem {
//public:
//	bool operator()( int elem )
//         { return elem%2 ? false : true; }
//};
class even_elem{
public:
    bool operator()(int elem){
        return elem%2 ? false:true;
    }
};
void test_4()
{
    vector input;
    istream_iterator in(cin),eos;

    //输出文件 偶数文件 奇数文件
    ofstream even_file("G:\QT_project\EssentialCPP\build-chap03-Desktop_Qt_5_9_8_MSVC2015_64bit-Debug\debug\even_file"),
    odd_file("G:\QT_project\EssentialCPP\build-chap03-Desktop_Qt_5_9_8_MSVC2015_64bit-Debug\debug\odd_file");

    if(!even_file|!odd_file){
        cerr << "arghh!! unable to open the output files. bailing out!";
        return ;
    }
    copy(in,eos,back_inserter(input));

    //泛型函数partition区分奇偶函数。当然同时配合 function object even_elem()的使用,后者在传入时为偶数是会返回true。
    vector::iterator division=partition(input.begin(),input.end(),even_elem());

    //再将这两个ostream_iterator绑定至相应的ofstream对象上。第二个参数代表每个元素输出时的分隔符
    ostream_iterator even_iter(even_file,"n"),
            odd_iter(odd_file," ");

    //最后 再以泛型函数copy(),将已经被分开的奇偶元素分别输出至不同的文件
    copy(input.begin(),division,even_iter);//从input开始取 division的前部分为偶数
    copy(division,input.end(),odd_iter);//division后半部分为奇数 直到input end

}



void ex3_4()
{
    vector< int > input;
    istream_iterator in( cin ), eos;

    ofstream even_file( "even_file" ),
              odd_file( "odd_file"  );

    if ( ! even_file || ! odd_file ){
         cerr << "arghh!! unable to open the output files. bailing out!";
         return ;
    }

    copy( in, eos, back_inserter( input ));
    vector::iterator division =
               partition( input.begin(), input.end(), even_elem() );

    ostream_iterator even_iter( even_file, "n" ),
                           odd_iter( odd_file, " "  );

    copy( input.begin(), division, even_iter );
    copy( division, input.end(), odd_iter );

}



int main()
{
    //test_3();
    //test_4();
    ex3_4();
    cout << "Hello World!" << endl;
    return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/867367.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号