insert/delete into std::map found/at by constants
I'm using json-spirit's mObject to create jagged multidimensional maps for
my family's real-time personal finance websocket server.
I've found that the [] operator doesn't accept consts, but I can't find
how to insert using find(const) or at(const).
With this code
string accountList[] = {"mine", "wife's", "kid's"};
BOOST_FOREACH( string account, accountList )
{
accounts[account] = mObject(); // no error
accounts.insert(std::make_pair(account,mObject())); // no error
accounts[account]["cash"] = mValue(0); //error
accounts.at(account).insert(std::make_pair("cash",mValue(0))); //
still error
}
The first usage of the [] operator strangely doesn't give error, thus I
assume it works. The second line inside the FOREACH also works, but I get
errors for the last two.
error: no match for 'operator[]' in 'accounts.std::map<_Key, _Tp, _Compare,
_Alloc>::operator[]<std::basic_string<char>,
json_spirit::Value_impl<json_spirit::Config_map<std::basic_string<char> > >,
std::less<std::basic_string<char> >, std::allocator<std::pair<const
std::basic_string<char>,
json_spirit::Value_impl<json_spirit::Config_map<std::basic_string<char> >
> > > >((*
(const key_type*)(& account)))["cash"]'
and
error: 'std::map<std::basic_string<char>,
json_spirit::Value_impl<json_spirit::Config_map<std::basic_string<char> > >,
std::less<std::basic_string<char> >, std::allocator<std::pair<const
std::basic_string<char>,
json_spirit::Value_impl<json_spirit::Config_map<std::basic_string<char> >
> > >
>::mapped_type' has no member named 'insert'
According to the json-spirit docs, mObject is a typedef std::map<
std::string, mValue > mObject;, and I'm pretty sure that mValue is some
sort of boost::any since it gave the same errors as when I tried to
reinvent the wheel.
How can I insert and (since I'm having so much trouble) delete into
mObjects with const strings as key argument?
No comments:
Post a Comment