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