# Problem with iteration through the map

**URL:** <https://root-forum.cern.ch/t/problem-with-iteration-through-the-map/43356>\
**Category:** Newbie\
**Created:** [February 1, 2021, 4:08pm UTC](https://root-forum.cern.ch/t/problem-with-iteration-through-the-map/43356 "2021-02-01T16:08:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alobasenko](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/97f17d/32.png) [@alobasenko](https://root-forum.cern.ch/u/alobasenko)\
**Post date:** [February 1, 2021, 4:08pm UTC](https://root-forum.cern.ch/t/problem-with-iteration-through-the-map/43356/1 "2021-02-01T16:08:41Z")

</div>

_ROOT Version:_ 6.20/00  
_Platform:_ Ubuntu 20.04.2 LTS  
_Compiler:_ gcc version 9.3.0

* * *

Hi,

I am stuck with the problem of iterating through the map object.  
Here is my code of the test macro:

```auto

#include <iostream>
#include <fstream>
#include <iomanip> 
#include <vector>
#include <string>
#include <map>
#include <set>

#include <algorithm>

#include <TMath.h>
#include <TSystem.h>

using namespace std;

void PrintMap(const map<int, double>& m){
	cout << "size = " << m.size() << endl;
	for (auto item : m){
		cout << item.first << " : " << item.second << endl;
	}
}

int test()
{
	map<int, double> new_m = {{1, 0}, {2, 0}, {3, 0}};
    new_m.at(1) = 111.;
    new_m.at(2) = 222.;
    new_m.at(3) = 333.;
    PrintMap(new_m);
    /*for(auto& item : new_m){
        cout << item.first << " ::: " << item.second << endl;
    }*/
	return 0;
}

```

After trying to run this code, I got such errors:

```auto
root [0] .L test.C
root [1] .x test.C++
Info in <ACLiC>: script has already been loaded in interpreted mode
Info in <ACLiC>: unloading /local/home/al264242/Documents/Files/Coursera/C++/white/./test.C and compiling it
Info in <TUnixSystem::ACLiC>: creating shared library /local/home/al264242/Documents/Files/Coursera/C++/white/./test_C.so
In file included from test_C_ACLiC_dict dictionary payload:8:
././test.C:19:17: error: cannot increment value of type 'std::_Rb_tree_const_iterator<std::pair<const int, double>
      >'
        for (auto item : m){
                       ^
././test.C:19:19: note: in implicit call to 'operator++' for iterator of type 'const std::map<int, double,
      std::less<int>, std::allocator<std::pair<const int, double> > >'
        for (auto item : m){
                         ^
/usr/include/c++/9/bits/stl_map.h:363:7: note: selected 'begin' function with iterator type 'std::map<int, double, std::less<int>,
      std::allocator<std::pair<const int, double> > >::const_iterator' (aka
      '_Rb_tree_const_iterator<std::pair<const int, double> >')
      begin() const _GLIBCXX_NOEXCEPT
      ^
Warning in <TInterpreter::TCling::RegisterModule>: Problems declaring payload for module test_C_ACLiC_dict.
input_line_102:2:3: error: use of undeclared identifier 'test'
 (test())
  ^
Error in <HandleInterpreterException>: Error evaluating expression (test()).
Execution of your code was aborted.

```

I could not understand such an error.  
If I change an object type inside the loop according to this:

```auto
for (pair<const int, double> item : m){
		cout << item.first << " : " << item.second << endl;
	}

```

an error is still present. What am I doing wrong?  
Thanks in advance!

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [February 1, 2021, 5:40pm UTC](https://root-forum.cern.ch/t/problem-with-iteration-through-the-map/43356/2 "2021-02-01T17:40:52Z")

</div>

Hi,  
I don’t have any problem running your code.  
But why you are doing:

```auto
.L test.C
.x test.C++

```

?  
You don’t need to run ACLIC after having parsed the code with Cling. Also with Cling you don’t really need to compile the code with ACLIC. Just use Cling:

```auto
.x test.C

```

Lorenzo

---

<div class="post-metadata">

**Author:** ![alobasenko](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/97f17d/32.png) [@alobasenko](https://root-forum.cern.ch/u/alobasenko)\
**Post date:** [February 1, 2021, 5:49pm UTC](https://root-forum.cern.ch/t/problem-with-iteration-through-the-map/43356/3 "2021-02-01T17:49:28Z")

</div>

Hi Lorenzo,

> [@moneta](#):
>
> Just use Cling:
> 
> ```auto
> .x test.C
> 
> ```

Indeed, that was a solution…  
Thank you very much for your explanation!

Andrii
