[英]copying vector of maps data into a map of vectors
抱歉再次发布相同的问题,但是我使用了建议的解决方案,并且部分起作用了...所以我想澄清一下问题:
所以:
我定义了一个地图矢量,如下所示:
typedef vector<map<string,unsigned int> > myvec;
以及矢量映射(我称之为索引),如下所示:
typedef map<string,vector<unsigned int> > index;
然后,我执行以下操作:
在名为myCoogle的课程中,我声明了myvec maps_vec;
我把地图填满了...
在每个映射中都有一个单词(字符串)和一个数字(未整数)。
到现在为止还挺好。
我还声明了index the_index;
现在,我要将所有不同的单词从the_vec复制到the_index。
单词将是字符串...
对于每个向量,我将添加存储在地图向量中的数字。
例如:
the_vec有3个地图。
第一个有:鸡肉1 | 人,1 | 电梯,5 | 是2 | ...
第二个有:person,2 | 冰淇淋3 | 是3 | ...
第三有:电梯,1 | 熊1 | 是4 | 鸡3 | ...
所以the_index应该看起来像这样:
单词,[整数向量]
鸡[1,0,3]
人,[1,2,0]
电梯[5,0,1]
是[2,3,4]
冰淇淋[0,3,0]
熊[0,0,1]
好的,这是我的功能:
void Coogle::make_index()
{
//SCAN THE FIRST MAP
myvec::iterator myvec_iter;
map<string,unsigned int>::iterator map_iter;
index::iterator idx_iter = the_index.begin();
for(map_iter=maps_vec[0].begin(); map_iter!=maps_vec[0].end(); ++map_iter)
{
the_index[map_iter->first].push_back(map_iter->second);
}
//SCAN THE OTHER MAPS
myvec_iter=maps_vec.begin();
myvec_iter++;
int i=0; //FILE #
while(myvec_iter!=maps_vec.end())
{
i++;
for(map_iter=maps_vec[i].begin(); map_iter!=maps_vec[i].end(); ++map_iter)
{
string word=map_iter->first;
cout << "DEALING WITH WORD \"" << word << "\"" << endl;
index::iterator location;
location=the_index.find(word);
if(location!=the_index.end()) //if word found in the index
{
cout << "WORD EXISTS!" << endl;
location->second[i]=map_iter->second;
}
else //if not found
{
cout << "WORD DOES NOT EXIST! NEW WORD." << endl;
the_index[word].push_back(map_iter->second);
}
}
cout << endl;
++myvec_iter;
}
}
澄清:FILE#是地图编号...我正在处理文件(* .txt文件)。
好了,所以我扫描完第一张地图后,尝试打印the_index,一切都很好。 但是我在扫描其他地图后尝试打印时得到了这个:
但是,“成功构建”。
当我运行程序时,该窗口弹出。
因此,我相信我的第二个“ for”循环出了问题。
有人可以帮忙吗?
非常长的帖子非常抱歉...
非常感谢你 !!!
编辑:如果我不尝试打印the_index,该程序将编译并运行正常。 但这当然还不够。 但是我的打印功能还可以...在这里:
void Coogle::print_index() const
{
index::const_iterator iter;
for(iter=the_index.begin();iter!=the_index.end();++iter)
{
cout << "Word: " << iter->first << endl;
//cout << "Files number is: " << files_number << endl; //prints: 3
for(int i=0; i<files_number;i++)
cout << "file #" << i+1 << ": " << iter->second[i] << " , " << endl;
}
}
编辑:
以下是仅打印1张地图与打印3张地图的屏幕截图:
编辑:好的,希望我终于明白了您要做什么。
typdef myvec::iter vmiter_t;
typdef map<string,unsigned int>::iter miter_t;
typdef set<string>::iter siter_t;
set<string> words;
for(vmiter_t vmiter=maps_vec.begin(); vmiter != maps_vec.end(); ++vmiter)
{
for(miter_t miter = vmiter->begin(); miter != vmiter->.end(); ++miter)
{
words.insert(miter->first);
}
}
for (siter_t iter=words.begin(); iter!=words.end(); ++iter){
const string& word = *iter;
for(vmiter_t vmiter=maps_vec.begin(); vmiter != maps_vec.end(); ++vmiter)
{
map<string,unsigned int>& temp = *vmiter;
the_index[word].push_back(temp[word]);
}
}
很难说,因为您没有提供变量的声明(其中有些似乎被错误命名),但是我认为问题出在行location->second[i]=map_iter->second;
。 您正在尝试访问向量中的索引i,但是向量实际上没有那么长。
编辑:现在,我可以告诉您您实际上要做什么,您就使事情变得过于复杂。 您根本不需要其他元素的单独循环。
这是C ++ 11的答案:
myvec v = /* ... */;
index q;
for (auto const & m : v)
for (auto const & p : m)
q[p->first].emplace_back(p->second);
它与@Antimony的解决方案相同,只是在眼睛上容易一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.