C++ Premier Plus 6th edition - Programming excercise - Chapter16 - 8

main.cpp

#include<iostream>
#include<vector> // in case one has friends having same name(first and last)
#include<set> // set can sort and remove duplicates
#include<string>
#include<fstream> // read 2 name-lists from files
#include<algorithm> // merge()
#include<iterator> // insert_iterator

using std::vector;
using std::string;
using std::set;
using std::cout;

// prototype
vector<string> readFile(const string& str, void(*f)(vector<string>&, string&));
void push(vector<string>& v, string& temp);
template<typename T>
void Show(T& t);
template<typename T>
void ShowAll(T& t, const char* c);

int main()
{
    // 1st container to store Mat's friends
    vector<string> Mat;
    Mat = readFile("Mat-List.txt", push);
    std::sort(Mat.begin(), Mat.end());
    ShowAll(Mat,"Mat");

    // 2nd container to store Pat's friends
    vector<string> Pat;
    Pat = readFile("Pat-List.txt", push);
    std::sort(Pat.begin(), Pat.end());
    ShowAll(Pat,"Pat");

    // merge 1st and 2nd containers into set
    set<string> setTotal;
    std::merge(Mat.begin(), Mat.end(),
               Pat.begin(), Pat.end(),
               std::insert_iterator<set<string>>(setTotal,setTotal.begin()));

    ShowAll(setTotal,"Mat and Pat");
    std::cin.get();
}

// function to read names from file, using a function pointer
/*
   usage of funtion pointer,must indicate return type and auguments type,
   so that any difinations having same prototype can be pointed by this pointer
*/
vector<string> readFile(const string& str, void (*f)(vector<string>&, string&))
{
    std::ifstream fin;
    fin.open(str); // txt file,names seperated by comma
    if (!fin.is_open())
    {
        std::cerr << "Can't open file \""<< str << "\". Bye.\n";
        system("pause");
        exit(EXIT_FAILURE);
    }

    // start to do task using contents from file
    vector<string> vec;
    while (fin.good())
    {
        char ch;
		// must put this statement inside of while,otherwise each string
		// will be longer and longer,new char appended to old temp
		string temp;
		// complete a temp
        while (!fin.eof())
        {
            fin.get(ch);
            if (ch == ',')
                break;
            temp.push_back(ch);
        }

		// push the temp into vec
        f = push;// function pointer points to a certain function
        (*f)(vec, temp);
    }
    // reading stopped,possible reasons
    if (fin.bad())
        cout << "I/O error while reading.\n";
    else if (fin.eof())
        cout << "End of file reached.\n";
    else if (fin.fail())
        cout << "Input terminated by file dismatch.\n";
    // return a copy,not reference,as vec will be released after the block
    return vec;
}

// function using contents from file
void push(vector<string>& v, string& temp)
{
    v.push_back(temp);
}

// below 3 functions is for show all elements
template<typename T>
void Show(T& t) // T shall be container's element - type
{
    cout << t << ", ";
}

template<typename T>
void ShowAll(T& t, const char* c) // T shall be container-type
{
    cout << "Friends list of " << c << " : \n";
    // DO PAY ATTENTION, without const, works for vector, but not for set
    // as key-value and value is same in set,requres key can't be modified
    // it's allowed to add const in front of type, though element is not const
    std::for_each(t.begin(), t.end(), Show<const string>);
    cout << "\n\n";
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值