Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I modify values in a map using range based for loop?

I have a range based for loop to iterate over elements in foobar as follows:

#include <map>
#include <iostream>

int main()
{
  std::map<int, int> foobar({{1,1}, {2,2}, {3,3}});

  for(auto p : foobar) 
  {
    ++p.second;
    std::cout << "{" << p.first << ", " << p.second << "} ";
  }
  std::cout << std::endl;

  for(auto q : foobar) 
  {
    std::cout << "{" << q.first << ", " << q.second << "} ";
  } 
  std::cout << std::endl;
}

This code produces the following output:

{1, 2} {2, 3} {3, 4}
{1, 1} {2, 2} {3, 3}

The first line is modified and printed inside a for loop and the second line supposedly prints the same modified values. Why don't the outputs match? Are changes to std::map only effective in the scope of the loop? Is there a way I can not only access but modify these values?

A running version of this code can be found on cpp.sh.

EDIT: The example given here was modified to match the accepted answer for clarity.

like image 983
intcreator Avatar asked Aug 03 '15 19:08

intcreator


2 Answers

You can turn auto into auto& if you want to mutate/modify the container, for instance:

#include <map>
#include <iostream>

int main()
{
  std::map<int, int> foobar({{1,1}, {2,2}, {3,3}});
  for(auto& p : foobar) {
    ++p.second;
    std::cout << '{' << p.first << ", " << p.second << "} ";
  }
  std::cout << std::endl;
}

compiles ands outputs

{1, 2} {2, 3} {3, 4} 

live example

like image 178
Caninonos Avatar answered Oct 21 '22 11:10

Caninonos


Plain auto is by value (you get a copy). Use auto&.

like image 21
Cheers and hth. - Alf Avatar answered Oct 21 '22 12:10

Cheers and hth. - Alf