问题描述
我已。
但是,例如。
当我使用
accounts [bank] [cash] = 100;
gcc
错误:accounts.std :: map _Alloc> ; :: operator []< std :: basic_string< char>,boost :: any,
std :: less< std :: basic_string< char> >,std :: allocator< std :: pair< const
std :: basic_string< char>,boost :: any> > >((*& std :: basic_string< char>((const
char *)bank),(*(const std :: allocator< char> *)(& std :: allocator< ; char>())))))[cash]'
如何使用锯齿状的多维map
使用boost :: any
创建? (
多维声明
strong>std :: map< std :: string,boost :: any>帐户;
accounts [bank] = std :: map< std :: string,boost :: any>();
accounts [bank] [cash] = 100;
并尝试使用json-spirit的
mObject
,因为所有这些似乎已经内置了。
有趣的是使用完全相同的符号,我得到完全相同的错误。
解决方案std :: map< std :: string,boost :: any>帐户;
accounts [bank] = std :: map< std :: string,boost :: any>();
accounts [bank] [cash] = 100;
当然这会导致编译时错误,你放在boost :: any std :: map,
但编译器不知道这个。 account [bank]有boost :: any类型,
和boost :: any没有int&阅读boost :: any的工作原理:
修正是微不足道的:
#include< boost / any.hpp>
#include< map>
#include< string>
int main()
{
std :: map< std :: string,boost :: any>帐户;
accounts [cash] = 100;
accounts [bank] = std :: map< std :: string,boost :: any>();
boost :: any_cast< std :: map< std :: string,boost :: any> &>(accounts [bank])[cash] = 100;
}
I've been shown how to create a jagged multidimensional
std::map
by usingboost::any
.However, I'm having trouble setting the values like in this answer.
When I use
accounts["bank"]["cash"] = 100;
gcc
gives this errorerror: no match for ‘operator[]’ in ‘accounts.std::map<_Key, _Tp, _Compare, _Alloc>::operator[]<std::basic_string<char>, boost::any, std::less<std::basic_string<char> >, std::allocator<std::pair<const std::basic_string<char>, boost::any> > >((* & std::basic_string<char>(((const char*)"bank"), (*(const std::allocator<char>*)(& std::allocator<char>())))))["cash"]’
How can a jagged multidimensional
map
created withboost::any
be accessed? (If there is a better technique to do this, please show me. I only care about what works and is quick to write.)multidimensional declaration
std::map<std::string, boost::any> accounts; accounts["bank"] = std::map<std::string, boost::any>(); accounts["bank"]["cash"] = 100;
I gave up and tried to use json-spirit's
mObject
instead since all of this seems already built in.Funny thing is is that with the exact same notation, I get the exact same error.
解决方案std::map<std::string, boost::any> accounts; accounts["bank"] = std::map<std::string, boost::any>(); accounts["bank"]["cash"] = 100;
Of course this cause compile time error, you put to boost::any std::map,but compiler have no idea about this. accounts["bank"] has "boost::any" type,and boost::any have no
int& operator[](const char *)
Read how boost::any works: http://www.boost.org/doc/libs/1_54_0/doc/html/any/s02.html
Fix is trivial:
#include <boost/any.hpp> #include <map> #include <string> int main() { std::map<std::string, boost::any> accounts; accounts["cash"] = 100; accounts["bank"] = std::map<std::string, boost::any>(); boost::any_cast<std::map<std::string, boost::any> &>(accounts["bank"])["cash"] = 100; }
这篇关于设置/访问由map< string,boost :: any>创建的锯齿地图值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!