十万火急!一道C++编程题

来源:百度知道 编辑:UC知道 时间:2024/07/07 13:12:40
从键盘读入一个文本文件名字(可带路径),为该文件中的所有单词建立一个词汇索引。按字母顺序显示所有单词(仅此一次),后面紧跟着它们所在的行号。大写与小写字母被认为是相同的。例如,对于下列的输入文件:
To be or
not to be,
that is the question.
产生的词汇索引如下:
be 1 2
is 3
not 2
or 1
question 3
that 3
the 3
to 1 2
我需要算法及具体的源程序

#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <vector>

using namespace std;

int main()
{
cout << "Input the file name :" << endl;

string filename;
cin >> filename;

ifstream file( filename.c_str() );
if ( !file.is_open() )
{
cout << "Fail to open file " << filename << endl;
return -1;
}

map<string, vector<int> > m;

char strline[100];
int linenum = 1;
char seps[] = " ,.!:\t\r\n\"\'";
char *token;
string s;
while ( file.getline( strline, sizeof(strline) ) )
{
strlwr( strline );
token = strtok( strline, seps );
while ( token != NULL )
{
s = token;
map<string, vector<int> >::iterator iter =