【发布时间】:2023-03-27 13:27:02
【问题描述】:
我知道map 是什么以及它的一般基本功能,但我不知道为什么在这里使用set 而不是仅仅声明int i = value 或类似的东西。
我真正想做的是:将单词放入向量后,我还想将同一个单词用作值的键。但我真的不知道使用map 这样做的全部目的。不确定我是否提供了足够的信息,但只是询问您还需要什么,我会回复。
我只提供了一个readWords 函数,但是如果有人需要完整的代码,包括头文件、类和主文件,那么我也可以把它们放上来。
在帮助下,我在那里写下了部分代码,但老实说,我不知道在 push_back() 函数之后它在做什么。
/* Read word-by-word from filename and store words in text vector.
* Also use normalized version of word as key in concordance map
* The value associated with each key in the map is a set whose
* keys are the associated indices into the vector.
*/
void Concordance::readWords(char * filename){
ifstream fin(filename, ifstream::in);
if (fin.is_open()){
while(!fin.eof()){
string word;
fin >> word;
normalize(word);
text.push_back(word); //puts word into vector
set<int> seat;
seat.insert(text.size()-1);
pair<string, set<int> > pear;
concordance.insert(pear);
}
}
else{
cerr << "Unable to open file datafile.txt";
exit(1); // call system to stop
}
fin.close(); //closes the filename
}
【问题讨论】: