include map include string include iostream include fstream typedef st

 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
#include <map>
#include <string>
#include <iostream>
#include <fstream>
typedef std::multimap<std::string, std::string> dictionary;
typedef std::pair<std::string, std::string> dict_pair;
int main()
{
std::ifstream in("info.txt");
in.exceptions(std::ifstream::eofbit);
dictionary dict;
// parse file
try{
bool nextWord = true;
std::string word;
while(!in.eof())
{
if(nextWord)
{
std::getline(in, word);
nextWord = false;
}
std::string trans;
std::getline(in, trans);
if(trans.empty())
{
nextWord = true;
continue;
}
dict.insert(dict_pair(word, trans));
}
}catch(std::ifstream::failure exc){
//TODO: May be illegal file format
}
dictionary::iterator itr = dict.begin();
for(; itr != dict.end(); itr++)
std::cout << itr->first << " => " << itr->second << std::endl;
in.close();
}