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:
#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:
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:
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!