-2

I have a std::map<string, int> and std::vector<vector<double>>. I have to iterate both the container at the same time doing this way. And i would like to update the value of 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]);
}

How can I iterate over both the container at the same time, so that i can update the value of 2D vector.

Community
  • 1
  • 1
Shravan40
  • 8,922
  • 6
  • 28
  • 48

1 Answers1

1

Const values cannot be assigned to. Since you declared it as const auto&, you cannot assign to it. In order to be able to assign to it, you need to declare it non-const:

for (auto& it : *vec_itr)
//   ^^^^^^^^
//   not const
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084