c++stringstdmapstdstringstd-pair

How to stringify the value of a variable?


Let's say we've got int abc=123;

I want to stringify both the variable name and the value. But using a macro like #define n(x) #x only stringifies the variable name itself. How can I stringify the value along with the variable name and efficiently access them together?


Solution

  • Storing them together in a std::map would do this.

    #define n(x) #x
    int abc=123;
    std::map<std::string,std::string> f;
    f[n(abc)]=std::to_string(abc);
    

    Now f["abc"] == "123" and this was what I wanted to do. Thanks for previous help.