multidimensional jagged map

て烟熏妆下的殇ゞ 提交于 2020-01-05 04:51:14

问题


I'm a c++ noob trying to build a personal finance websocket server that tracks all of my assets and liabilities in real-time.

I've found that I can make maps of maps to have a multi-dimensional system of key-value pairs.

I've also found that boost::any and boost::variant can be used to store multiple types for a values. My problem is that some levels aren't very complex compared to others. For example, a bank account will only have a value, the amount in the account, while a brokerage account will have many types of investments and characteristics, so I'd like to do something like (in json):

{
    'bankAccount': 100.00,
    'brokerageAccount': {
        'stocks': {
            'companyName': 'Stack Exchange',
            'ticker': 'STAK',
            'pps': bazillion
            ...

Where bankAccount and brokerageAccount can be inserted and erased when needed and discarded as necessary.

I don't really know where to go from here. When I try to put

map<string, boost::any> accounts;
accounts["cash"] = 100;
accounts["brokerageAccount"] = map<string, boost::any>;

in the private section of broadcast_server in this websocket server, gcc with these flags -I ~/websocketpp-master/ -std=c++0x -D_WEBSOCKETPP_CPP11_STL_ -D_WEBSOCKETPP_NO_CPP11_REGEX_ -lboost_regex -lboost_system -L/usr/lib -pthread -O0 -ljson_spirit gives error: ‘accounts’ does not name a type for the last two lines.

How best can I store data in a json type way above, with the ability to add and delete keys and values anywhere?


回答1:


accounts["brokerageAccount"] = map<string, boost::any>;

You can not assign type to object. To fix problem add ()

accounts["brokerageAccount"] = map<string, boost::any>();

Variant that should be compiled correctly is:

#include <boost/any.hpp>
#include <map>
#include <string>

int main()
{
   std::map<std::string, boost::any> accounts;
   accounts["cash"] = 100;
   accounts["brokerageAccount"] = std::map<std::string, boost::any>();
}



回答2:


map<string, boost::any> on the last line is a type, not an object of that type. You have to call the constructor of that type to create an argument. Change the last line to

accounts["brokerageAccount"] = map<string, boost::any>();

This fixes it on my copy of Visual Studio 2010



来源:https://stackoverflow.com/questions/18805444/multidimensional-jagged-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!