int main()
{
ifstream fin;
fin.open("kriti1.txt");
map> index_map;
string line, word, page;
int pos;
getline(fin, line);
while (fin)
{
word = "";
page = "";
pos = line.find('{');
for (int i = pos + 1; line[i] != '}' && line[i] != '|'; i++) //读取目录项
{
word += line[i];
}
pos = line.find('{', pos + 1);
for (int i = pos + 1; line[i] != '}'; i++)//读取页码
{
page += line[i];
}
index_map[word].insert(page);
getline(fin, line);
}
for (const auto& x : index_map)
{
string temp = x.first;
pos = temp.find('!');
while (pos != string::npos)//目录项每次遇到一个“!”输出一个制表符
{
cout << "t";
temp = temp.substr(pos + 1);
pos = temp.find('!');
}
cout << temp << ": ";
page = "";
if (temp.find("constant") != string::npos) //目录项如果是constant类型,页码依序输出。否则如果目录项只有一个页码,输出该页码,如果有多个页码,输出范围。
{
for (const auto& y : x.second)
{
page += y + ", ";
}
page.erase(page.size() - 2, 2);
}
else
{
if (x.second.size() == 1)
{
page = *x.second.begin();
}
else
{
page = *x.second.begin() + '-' + *(--x.second.end());
}
}
cout << page << endl;
}
fin.close();
}