#pragma warning(disable:4786)
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <set>
#include <map>
using namespace std;
class CWord
{
string word;
public:
CWord(string word)
{
this->word = word;
}
string GetWord() const { return word; }
bool operator < (const CWord &w) const
{
return word < w.GetWord();
}
bool operator == (const string &s) const
{
return word == s;
}
};
class CWordSet
{
set<CWord> wordset;
public:
bool AddString(string s)
{
wordset.insert(CWord(s));
return true;
}
void Show(ostream &os)
{
set<CWord>::iterator it = wordset.begin();
int n = 0 ;
while(it != wordset.end())
{
os << (*it).GetWord() << "\t";
n ++;
if(n % 8 == 0)
{
os << endl;
n = 0;
}
it ++;
}
}
};
class CWordMap
{
map<CWord,int> wordmap;
public:
bool AddString(string s)
{
map<CWord,int>::iterator it = wordmap.find(s);
if(it == wordmap.end())
{
pair<CWord,int> p(CWord(s),1);
wordmap.insert(p);
}
else
{
(*it).second += 1;
}
return true;
}
void Show(ostream &os)
{
map<CWord,int>::iterator it = wordmap.begin();
while(it != wordmap.end())
{
string ss = ((*it).first).GetWord();
int n = (*it).second;
os << ((*it).first).GetWord() << "\t" << (*it).second <<endl;
it ++;
}
}
};
int main()
{
CWordSet wordset;
CWordMap wordmap;
int pos = 0;
string s = "";
string delimset = ",.";
ifstream in("1.txt");
while(!in.eof())
{
getline(in,s);
if(s == "")
continue;
pos = 0;
while((pos=s.find_first_of(delimset,pos)) != string::npos)
s.replace(pos,1," ");
istringstream stringeam(s);
while(!stringeam.eof())
{
stringeam >> s;
if(s == "")
continue;
wordset.AddString(s);
wordmap.AddString(s);
}
}
in.close();
cout << "word set : " << endl;
wordset.Show(cout);
cout << endl;
cout << "word and times : "<< endl;
wordmap.Show(cout);
cout << endl;
return 0;
}