<legend id='7Kib7'><style id='7Kib7'><dir id='7Kib7'><q id='7Kib7'></q></dir></style></legend>

    <i id='7Kib7'><tr id='7Kib7'><dt id='7Kib7'><q id='7Kib7'><span id='7Kib7'><b id='7Kib7'><form id='7Kib7'><ins id='7Kib7'></ins><ul id='7Kib7'></ul><sub id='7Kib7'></sub></form><legend id='7Kib7'></legend><bdo id='7Kib7'><pre id='7Kib7'><center id='7Kib7'></center></pre></bdo></b><th id='7Kib7'></th></span></q></dt></tr></i><div id='7Kib7'><tfoot id='7Kib7'></tfoot><dl id='7Kib7'><fieldset id='7Kib7'></fieldset></dl></div>
  1. <tfoot id='7Kib7'></tfoot>
  2. <small id='7Kib7'></small><noframes id='7Kib7'>

      <bdo id='7Kib7'></bdo><ul id='7Kib7'></ul>

      你如何制作异构 boost::map?

      how do you make a heterogeneous boost::map?(你如何制作异构 boost::map?)

            • <bdo id='QbkX5'></bdo><ul id='QbkX5'></ul>

              <tfoot id='QbkX5'></tfoot>

                <tbody id='QbkX5'></tbody>
              <i id='QbkX5'><tr id='QbkX5'><dt id='QbkX5'><q id='QbkX5'><span id='QbkX5'><b id='QbkX5'><form id='QbkX5'><ins id='QbkX5'></ins><ul id='QbkX5'></ul><sub id='QbkX5'></sub></form><legend id='QbkX5'></legend><bdo id='QbkX5'><pre id='QbkX5'><center id='QbkX5'></center></pre></bdo></b><th id='QbkX5'></th></span></q></dt></tr></i><div id='QbkX5'><tfoot id='QbkX5'></tfoot><dl id='QbkX5'><fieldset id='QbkX5'></fieldset></dl></div>

              <small id='QbkX5'></small><noframes id='QbkX5'>

                <legend id='QbkX5'><style id='QbkX5'><dir id='QbkX5'><q id='QbkX5'></q></dir></style></legend>

                本文介绍了你如何制作异构 boost::map?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想要一个具有同构键类型但具有异构数据类型的映射.

                I want to have a map that has a homogeneous key type but heterogeneous data types.

                我希望能够做一些类似(伪代码)的事情:

                I want to be able to do something like (pseudo-code):

                boost::map<std::string, magic_goes_here> m;
                m.add<int>("a", 2);
                m.add<std::string>("b", "black sheep");
                
                int i = m.get<int>("a");
                int j = m.get<int>("b"); // error!
                

                我可以有一个指向基类的指针作为数据类型,但我宁愿没有.

                I could have a pointer to a base class as the data type but would rather not.

                我以前从未使用过 boost,但查看了fusion 库但不知道我需要做什么.

                I've never used boost before but have looked at the fusion library but can't figure out what I need to do.

                感谢您的帮助.

                推荐答案

                #include <map>
                #include <string>
                #include <iostream>
                #include <boost/any.hpp>
                
                int main()
                {
                    try
                    {
                        std::map<std::string, boost::any> m;
                        m["a"]  = 2;
                        m["b"]  = static_cast<char const *>("black sheep");
                
                        int i = boost::any_cast<int>(m["a"]);
                        std::cout << "I(" << i << ")
                ";
                
                        int j = boost::any_cast<int>(m["b"]); // throws exception
                        std::cout << "J(" << j << ")
                ";
                    }
                    catch(...)
                    {
                        std::cout << "Exception
                ";
                    }
                
                }
                

                这篇关于你如何制作异构 boost::map?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)
                Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)
                STL BigInt class implementation(STL BigInt 类实现)
                Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)
                Move list element to the end in STL(在 STL 中将列表元素移动到末尾)
                Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)

                  • <small id='SrSbZ'></small><noframes id='SrSbZ'>

                    <i id='SrSbZ'><tr id='SrSbZ'><dt id='SrSbZ'><q id='SrSbZ'><span id='SrSbZ'><b id='SrSbZ'><form id='SrSbZ'><ins id='SrSbZ'></ins><ul id='SrSbZ'></ul><sub id='SrSbZ'></sub></form><legend id='SrSbZ'></legend><bdo id='SrSbZ'><pre id='SrSbZ'><center id='SrSbZ'></center></pre></bdo></b><th id='SrSbZ'></th></span></q></dt></tr></i><div id='SrSbZ'><tfoot id='SrSbZ'></tfoot><dl id='SrSbZ'><fieldset id='SrSbZ'></fieldset></dl></div>
                        <tbody id='SrSbZ'></tbody>
                        <bdo id='SrSbZ'></bdo><ul id='SrSbZ'></ul>
                      • <tfoot id='SrSbZ'></tfoot>

                        <legend id='SrSbZ'><style id='SrSbZ'><dir id='SrSbZ'><q id='SrSbZ'></q></dir></style></legend>