jeudi 25 novembre 2021

output map object where the value can be any data type

Trying to output a map object where the value can be any data type. Tried the following:

#include <iostream>
#include <unordered_map>
#include <any>

std::unordered_map<std::string, std::any> example = {
    {"first", 'A'},
    {"second", 2},
    {"third", 'C'}
};

std::ostream &operator<<(std::ostream &os,
                    const std::any &m) {
 for (auto &t : example) {
    os << "{" << t.first << ": " << t.second << "}\n";
}
return os;
}

 int main()
{std::cout << example;
 return 0;
}

But,getting infinite loop of values.

Aucun commentaire:

Enregistrer un commentaire