【问题标题】:Constructing a Map With Vectors as Values构建以向量为值的映射
【发布时间】:2023-03-21 01:30:02
【问题描述】:

我正在尝试构建一个将字符串映射到无符号整数向量的映射。我构建这张地图的方式如下:

void PixelP1ROCDACSettings::getDACs(map<string,vector<unsigned int>>& dacs) const
{
    dacs.clear();
    dacs.insert(pair<string, vector<unsigned int> > (k_DACName_Vdd, make_vector(Vdd_, k_DACAddress_Vdd)));
    dacs.insert(pair<string, vector<unsigned int> > (k_DACName_Vana,make_vector(Vana_, k_DACAddress_Vana)));
    ...
}

其中make_vector定义如下:

   std::vector<unsigned int> make_vector(unsigned int DACValue, 
                                         unsigned int DACAddress) const ;

我的问题是: 1)我想访问我的向量中的每个单独的值,我试图这样做, dacs[key][index] 但这似乎不起作用。有没有特殊的语法可以做到这一点?

2) 另外,我想遍历我的地图,我该怎么做?

提前致谢。

【问题讨论】:

标签: c++ dictionary vector iterator


【解决方案1】:

如果你使用的是 c++11,你可以迭代

for (auto& keyvaluepair : dacs) {
     //keyvaluepair.first is your string
     //keyvaluepair.second is your vector
}

还有 dacs[key][index] 是访问 key 映射到的向量中第 index 个元素的正确方法。

【讨论】:

  • 我实施了你的建议,但现在我收到以下错误:>src/common/PixelFECInterface.cc:2674: error: expected initializer before ‘:’ token
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-09
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多