【发布时间】:2019-01-24 11:07:44
【问题描述】:
我需要使用map<string,int>从文件中取出出现的单词,然后我需要将它们复制到map<int,
vector<string>, cmpDec >,并按降序打印。
我尝试将文件中的词频提取到map<string, int>,然后尝试将其复制到map<int,
vector<string> >,但没有结果
我已经声明了 2 张地图:
map<string, int> text;
map<int, vector<string>, cmpDec> freq;
我从第一个地图中的文件中获取带有单词频率的文本:
while (rf >> words) {
text[words]++;
}
现在我必须将频率放在第二张地图中(必需),其中我需要第一个 int,用于单词频率的数量,vector 与每个频率的单词,以及降低频率的比较.
现在我正在尝试通过以下方式将数据从第一张地图中放入第二张地图中:
map<string, int>::iterator iter_map1 = text.begin();
map<int, vector<string>>::iterator iter = freq.begin();
vector<string>::iterator iter_v;
for (; iter_map1 != text.end(); ++iter_map1) {
iter->first.insert(make_pair(iter_map1->second, iter->second.push_back(iter_map1->first)));
}
它在 iter->second.... 行给出 2 个错误:
...\BagOfWords.cpp|56|error: request for member 'insert' in 'iter.std::_Rb_tree_iterator<_Tp>::operator-><std::pair<const int, std::vector<std::__cxx11::basic_string<char> > > >()->std::pair<const int, std::vector<std::__cxx11::basic_string<char> > >::first', which is of non-class type 'const int'|
和
...\BagOfWords.cpp|56|错误:无效表达式的使用|
我做错了什么?有没有更简单的方法可以从文件中获取单词(及其频率)并将它们放在第二张地图上,而无需从第一张地图中传递?
【问题讨论】:
-
使用
freq[cnt]插入它们有什么问题?
标签: c++ dictionary vector stl