Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C++ by blackhole ( 7 years ago )
#include <iostream>
#include <unordered_map>
#include <cstdlib>
#include "boost/variant.hpp"

class my_visitor : public boost::static_visitor<void>
{
public:
my_visitor(const std::string& s) : _s(s) {}
    void operator()(int i) 
    {
        std::cout << "key=" << _s << " value=" << i << std::endl;
    }

    void operator()(const std::string & str) 
    {
        std::cout << "key=" << _s << " value=" << str << std::endl;
    }
    private:
    std::string _s;
};


int main()
{
    typedef boost::variant< int, std::string > MyVariant;
    typedef std::unordered_map<std::string, MyVariant> MyMap;

    MyMap p;
    p["key1"] = MyVariant(1);
    p["key2"] = MyVariant("testing");
    for( const auto& n : p ) {
        my_visitor v(n.first);
        boost::apply_visitor(v, n.second);
    }

}

 

Revise this Paste

Your Name: Code Language: