TMap class

hello! I have 2 questions,

firstly, is it possible to use maps in root?

secondly, how the function add of TMap class is used? What the arguments must be?

can somebody give me an example?

I want to run with root a code like this

[code]#include
#include
#include

using namespace std;

struct cluster{
int n;
} J12;

map <string, cluster> mymap;

 int main(){

mymap[“J12”] = J12;

mymap[“J12”].n=50;

cout<<mymap[“J12”].n<<endl;

}[/code]

Hi,

Of course.

You can find the documentation of the Add methods here:

For what concerns your code, I am not sure I understand what you mean.
If you use these lines (extracted from your example) as a macro or even directly in the interpreter, they work:

struct cluster {
   int n;
} J12;
map <string, cluster> mymap;
mymap["J12"] = J12;
mymap["J12"].n=50;
cout<<mymap["J12"].n<<endl;

Cheers,
Danilo

hi danilo, thanks for your response!

Yes if i compile and run them they work, but if I run them from root I take this:

Error: Can’t call map<string,cluster,less,allocator<pair<const string,cluster> > >::operator in current scope C:\root_v5.34.32\map.cpp(19)
Possible candidates are…
(in map<string,cluster,less,allocator<pair<const string,cluster> > >)
Error: improper lvalue C:\root_v5.34.32\map.cpp(19)
(struct cluster)47185120
*** Interpreter error recovered ***

so I am looking for a way to solve this problem…

Hi,

since you are using ROOT5, probably the simplest option is to use ACLiC (root.cern.ch/compiling-your-cod … nown-aclic)
This version of your code, called test.C

#include <iostream>
#include <map>

struct cluster {
   int n;
};

void test(){
 cluster J12;
 map <string, cluster> mymap;
 mymap["J12"] = J12;
 mymap["J12"].n=50;
 cout<<mymap["J12"].n<<endl;
}

can be executed at the root prompt with this syntax:
root [0] .x test.C+

Cheers,
Danilo

ok, but I don’t want to run this particular code, I wrote it as an example for my question. I want to learn how to use maps in root, original c++ maps or root maps from Tmap.

With c++ maps I have the problem I described above…

In TMap I cannot understand how to use add function… What the arguments must be for a map with a string as key and a structure as value?

Hi,

If you want to use std maps, you’ll need a dictionary or use aclic as we saw in the previous post.
If you want to use tmap, you have to use as key a pointer to a TObjString object and you should convert your struct to a class inheriting from TObject. TMap requires keys and values to be TObject pointers.

D

ok, I will try to make a dictionary! thanks! happy new year!

root.cern.ch/how/how-create-dictionary

Let us know ho it goes.

Happy new year.

D