错误:分配只读参考'it'?

时间:2017-02-20 23:28:15

标签: c++ c++11

我有std::map<string, int>std::vector<vector<double>>。我必须同时迭代容器doing this way。我想更新2-D std::vector

的值
map<string,int> portfolioCategories = optimizationPortfolioCategories();
vector<vector<double>> coVarianceMatrix(numberOfCategory,vector<double>(numberOfCategory));
auto map_itr = portfolioCategories.begin();
auto vec_itr = coVarianceMatrix.begin();
for(; map_itr != portfolioCategories.end() && vec_itr != coVarianceMatrix.end(); map_itr++, vec_itr++) {
    for(const auto& it: (*vec_itr))
        it = coVariance(monthlyReturnFundCategory[index],monthlyReturnFundCategory[count]);
}

如何同时迭代两个容器,以便我可以更新2D矢量的值。

1 个答案:

答案 0 :(得分:1)

无法分配Const值。由于您将it声明为const auto&,因此您无法指定it。为了能够分配给it,您需要将其声明为非const:

for (auto& it : *vec_itr)
//   ^^^^^^^^
//   not const