include set include iostream include fstream include algorithm include

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <set>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
bool comparer(string a, string b)
{
return a.length() < b.length();
}
int main()
{
set<string> words;
ifstream file;
string filename;
cout << "Enter file name: ";
cin >> filename;
file.open(filename.c_str());
string word;
while (!file.eof())
{
char ch;
file.read(&ch, 1);
if (ch>64 && ch < 123) word += ch;
else
{
if (word.length())
{
words.insert(word);
word.clear();
}
}
}
vector<string> words2;
for(set<string>::iterator i = words.begin(); i != words.end(); i++)
{
words2.push_back(*i);
}
sort(words2.begin(), words2.end(), comparer);
for(vector<string>::iterator i = words2.begin(); i != words2.end(); i++)
{
cout << (*i) << endl;
}
}