Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 需要C+方面的帮助+;使用映射跟踪输入文件中的单词_C++_Stl_Maps - Fatal编程技术网

C++ 需要C+方面的帮助+;使用映射跟踪输入文件中的单词

C++ 需要C+方面的帮助+;使用映射跟踪输入文件中的单词,c++,stl,maps,C++,Stl,Maps,假设我有一个文本文件 today is today but tomorrow is today tomorrow 那么,如何使用地图来跟踪重复的单词呢?它在哪一行重复? 到目前为止,我已将文件中的每个字符串作为临时值读入,并按以下方式存储: map<string,int> storage; int count = 1 // for the first line of the file if(infile.is_open()){ while( !

假设我有一个文本文件

today is today but
tomorrow is today tomorrow
那么,如何使用地图来跟踪重复的单词呢?它在哪一行重复? 到目前为止,我已将文件中的每个字符串作为临时值读入,并按以下方式存储:

    map<string,int> storage;

    int count = 1 // for the first line of the file

    if(infile.is_open()){
     while( !infile.eof() ){ 
      getline(in, line);
      istringstream my_string(line);
      while(my_string.good()){
         string temp;
         my_string >> temp;

    storage[temp] = count
    }
    count++;// so that every string read in the next line will be recorded as that line.
}
}
   map<string,int>::iterator m;
   for(int m = storage.begin(); m!= storage.end(); m++){
      out<<m->first<<": "<<"line "<<m->second<<endl;
}
但是。。 它应该打印出来(没有重复的字符串):

注意:字符串的顺序无关紧要


任何帮助都将不胜感激。谢谢。

您试图从集合中获取2项信息,而您只在其中存储了1项信息

扩展当前实现的最简单方法是存储一个struct而不是int

因此,不是:

storage[temp] = count
你会:

storage[temp].linenumber = count;
storage[temp].wordcount++;
定义地图的地方:

struct worddata { int linenumber; int wordcount; };
std::map<string, worddata> storage;
struct-worddata{int-linenumber;int-wordcount;};
地图存储;
使用以下命令打印结果:

out << m->first << ": " << "line " << m->second.linenumber << " count: " << m->second.wordcount << endl;
out first映射存储具有唯一键的(键、值)对。这意味着,如果多次分配给同一个键,则只会存储最后分配的值

听起来您想做的不是将行存储为值,而是存储另一个行映射->发生

所以你可以这样制作你的地图:

typedef int LineNumber;
typedef int WordHits;
typedef map< LineNumber, WordHits> LineHitsMap;
typedef map< string, LineHitsMap > WordHitsMap;
WordHitsMap storage;

您的存储数据类型不足以存储所有要报告的信息。您可以通过使用向量进行计数存储来实现这一点,但您必须做大量的簿记工作,以确保在未遇到单词时实际插入0,并在遇到新词时创建大小正确的向量。这不是一件小事

你可以把你的计数部分转换成数字地图,第一个是线,第二个是计数。。。这将降低代码的复杂性,但并不是最有效的方法

无论如何,你不能用一个std::map做你需要做的事情

编辑:刚刚想到一个更容易生成但更难报告的替代版本:std::vector。对于文件中的每一行,您将生成一个新的贴图并将其推送到向量上。您可以创建一个助手类型集,以包含文件中显示的所有单词,以便在报告中使用

不管怎样,我可能就是这样做的,除非我将所有这些垃圾封装在一个类中,这样我就可以做如下事情:

my_counter.word_appearance(word,line_no);
while( getline(in, line) ){ 
      istringstream my_string(line);
      string temp;
      while(my_string >> temp ){
           // do something with temp
      }
}

除此之外,您的循环都是错误的。您应该决不在eof或good标志上循环,而是在读取操作成功时循环。你想要的是:

my_counter.word_appearance(word,line_no);
while( getline(in, line) ){ 
      istringstream my_string(line);
      string temp;
      while(my_string >> temp ){
           // do something with temp
      }
}

我认为用户想要做的是报告一个单词在文件的每一行中出现的次数。如果他们将你的数据类型插入到他们的算法中,他们将得到一个单词出现的最后一行以及它出现在文件中的总计数。嗯,我这样做了,但是遍历映射的循环似乎不再适用于(m=storage.begin();m!=storage.end();m++){错误C2679:binary'=':找不到接受类型为'std::的右操作数的运算符。时间已晚,我刚从发布处回来-计算出用户想要的算法太多:)。@eNetik-您需要修改迭代器定义以匹配原始映射定义。提示:使用typedef。(我将更新我的答案)我如何存储行号和单词点击数?你能举一个简单的例子说明如何实际将其存储到地图中吗?他可以使用多重地图存储每个单词的多个实例,使用count()获得单词数,并存储每次发生的行号如果这是作业,请将其标记为这样。
my_counter.word_appearance(word,line_no);
while( getline(in, line) ){ 
      istringstream my_string(line);
      string temp;
      while(my_string >> temp ){
           // do something with temp
      }
}