访问矢量中的地图:map<string,vector<map<string,矢量<int>> > >
Get access to map in a vector: map<string, vector<map<string, vector<int> > > >
我有以下自己创建的数据类型:
typedef map<string, vector<map<string, vector<int> > > > Entry;
表示以下数据结构:
Filename | vector<map<Word, vector<LineNumber> > >
表示每个FileName包含许多单词,每个单词包含linNumbers。它代表了多个文件中单词的索引。
为此假设我编写了一个函子,它应该将fileName, word和lnr添加到我的数据结构中:
FileWordLineAdder(string fileName, Entry wordLnr, int lnr) : fileName(fileName), entries(wordLnr), lnr(lnr) {
}
void operator()(string word) {
word = normalize(word);
if (word.size() == 0)
return;
vector<map<string, vector<int> > >& wordLnr = entries[fileName];
/* PROBLEM START */
//vector<int>& lnrs = wordLnr[word];
//lnrs.push_back(lnr);
/* PROBLEM END*/
} // operator()
现在我能够将fileName插入到我的数据结构中(参见上面的问题部分),但我无法访问向量中的映射。
有谁能帮帮我吗
我认为你的数据结构设计过度了。对于"file -> word -> linenumbers"的索引,只需:
Filename | map<Word, vector<LineNumber> >
。
map<string/*filename*/, map<string/*word*/, vector<unsigned long/*lineNumber*/ > > >
可能就足够了。请注意,这个数据结构已经是整个索引。它不是"一个条目",所以你的typedef可能命名错误。
(请注意,我删除了一个级别的"矢量"是在地图上-我认为这是你的问题的实际来源。对于vector<map<...>>
,您将有许多用于文件名的单词映射,并且您可能不知道该选择哪一个映射。
我的方案如下:
typedef map<string/*word*/, vector<unsigned long> /*lineNumber*/> IndexInnerMap;
typedef map<string/*filename*/, IndexInnerMap> Index;
// Get innerMap of specific fileName
IndexInnerMap& wordLnr = index[fileName];
// Get vector<lineNumber> of specific word
vector<unsigned long>& lnrs = wordLnr[word];
// Add line number to vector
if (lnrs.empty() || lnrs.back() != lnr) {
lnrs.push_back(lnr);
} // if
谢谢你的帮助;)
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- EASTL矢量<向量<int>>连续的
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 什么是模板&lt;&gt;inline bla bla
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是std :: set&lt; std :: future&gt;不可能存在
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾
- opencv 2.4.7在iOS错误背景_segm.hpp #include&lt; list&gt;未找到
- 在修改列表后,std :: list&lt; t&gt; :: end()的值是否会更改
- ///<评论></评论>在Visual Studio中