迭代时跳过相同的多重图片值 [英] Skip same multimap values when iterating
本文介绍了迭代时跳过相同的多重图片值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有什么好的方法来实现所需的输出,而不必删除相同的值或创建另一个列表/向量等?我试图将在不同文档中找到的单词映射到其所需输出中显示的文档名称。
Is there any good way to achieve the desired output below without having to remove the same values or creating another list/vector etc? I am trying to map words found in different documents to their document names as shown in the desired output.
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <sstream>
using namespace std;
multimap<string,string> inverts;
multimap<string,string>::iterator mit;
multimap<string,string>::iterator rit;
pair<multimap<string,string>::iterator,multimap<string,string>::iterator> ret;
int main(int argc, char* argv[])
{
ifstream infile;
for(int i=1;i<argc;i++)
{
char* fname=argv[i];
char line[1024];
string buff;
infile.open(fname);
while(infile.getline(line,1024))
{
stringstream ss(line);
while(ss >> buff)
inverts.insert(pair<string,string>(buff,fname));
}
infile.close();
}
for(mit=inverts.begin();mit!=inverts.end();mit++)
{
string first=(*mit).first;
cout<<first;
ret=inverts.equal_range(first);
for(rit=ret.first;rit!=ret.second;rit++)
cout<<" "<<(*rit).second;
cout<<endl;
}
return 0;
}
Output is:
> ./a.out A B
cat A
dog A B
dog A B
fox A B
fox A B
lion B
I need this output:
> ./a.out A B
cat A
dog A B
fox A B
lion B
推荐答案
您可以将 inverts
变更为 map< string,set< string> ;>
,将每个词映射到出现的文件名集。
You could change inverts
into map<string,set<string>>
, mapping each word to the set of file names where it appears.
这篇关于迭代时跳过相同的多重图片值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文