-2

当我提到ticker我有

['BTC_BBR','0.00069501','0.00074346','0.00069501','-0.00742634','8.63286802','11983.47150109',0,'0.00107920','0.00045422']

我需要将每个单元格写入一个单独的数组,例如
Arr1.push(BTC_BBR)
Arr2.push(0.00069501)
等等

如何连接可以通过参考
高速公路学习的图书馆

#include <autobahn/autobahn.hpp>
#include <autobahn/wamp_websocketpp_websocket_transport.hpp>
#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>
#include <boost/asio.hpp>
#include <boost/version.hpp>
#include <iostream>
#include <memory>
#include <string>
#include <tuple>


typedef websocketpp::client<websocketpp::config::asio_tls_client> client;
typedef autobahn::wamp_websocketpp_websocket_transport<websocketpp::config::asio_tls_client> websocket_transport;

void on_topic1(const autobahn::wamp_event& event)
{
    std::cerr << "received event: " << event.argument<uint64_t>(0) << std::endl;
}

int main(int argc, char const *argv[])
{

   try {

    boost::asio::io_service io;

    client ws_client;
    ws_client.init_asio(&io);
    ws_client.set_tls_init_handler([&](websocketpp::connection_hdl) {
        return websocketpp::lib::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::tlsv12_client);
    });
    auto transport = std::make_shared < autobahn::wamp_websocketpp_websocket_transport<websocketpp::config::asio_tls_client> >(
            ws_client, "wss://api.poloniex.com:443", true);

    auto session = std::make_shared<autobahn::wamp_session>(io, true);

    transport->attach(std::static_pointer_cast<autobahn::wamp_transport_handler>(session));

    boost::future<void> connect_future;
    boost::future<void> start_future;
    boost::future<void> join_future;
    boost::future<void> subscribe_future;
    connect_future = transport->connect().then([&](boost::future<void> connected) {
        try {
            connected.get();
        } catch (const std::exception& e) {
            std::cerr << e.what() << std::endl;
            io.stop();
            return;
        }

        std::cerr << "transport connected" << std::endl;

        start_future = session->start().then([&](boost::future<void> started) {
            try {
                started.get();
            } catch (const std::exception& e) {
                std::cerr << e.what() << std::endl;
                io.stop();
                return;
            }

            std::cerr << "session started" << std::endl;

            join_future = session->join("realm1").then([&](boost::future<uint64_t> joined) {
                try {
                    std::cerr << "joined realm: " << joined.get() << std::endl;
                } catch (const std::exception& e) {
                    std::cerr << e.what() << std::endl;
                    io.stop();
                    return;
                }

                subscribe_future = session->subscribe("ticker", &on_topic1).then([&] (boost::future<autobahn::wamp_subscription> subscribed)
                {
                    try {
                        std::cerr << "subscribed to topic: " << subscribed.get().id() << std::endl;
                        std::cerr << "---------------------" << argc <<std::endl;
                    }
                    catch (const std::exception& e) {
                        std::cerr << e.what() << std::endl;
                        io.stop();
                        return;
                    }
                });
            });
        });
    });

    std::cerr << "starting io service" << std::endl;
    io.run();
    std::cerr << "stopped io service" << std::endl;
}
catch (std::exception& e) {
    std::cerr << "exception: " << e.what() << std::endl;
    return 1;
}

    return 0;
}
4

1 回答 1

0

程序员将此任务称为“解析文本”。您想将文本解析为结构(请参阅@Someprogrammerdude 的评论)。

假设我们要将数据放入一个简单的结构中:

struct TickerEntry {
    std::string symbol;
    double a, b, c, d, e, f; // I have no clue,
    int i;                   // but already my names are
    double x, y;             // better than Arr1 and Arr2!
};

所以,我们想做一个像

TickerEntry parse_ticker_entry(std::string const& input);

它可以处理引号 , [,]并将所有数字-y 的东西(正和负无穷大、NaN、科学记数法等)转换为我们结构中的正确类型。

演示

这是使用我选择的武器的示例:Boost Spirit,它是一个解析器生成器。使用解析器生成器可以避免自己编写所有繁琐的检查。

Live On Coliru

#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/adapted/struct.hpp>

struct TickerEntry {
    std::string symbol;
    double a, b, c, d, e, f; // I have no clue,
    int i;                   // but already my names are
    double x, y;             // better than Arr1 and Arr2!
};

BOOST_FUSION_ADAPT_STRUCT(TickerEntry, symbol, a, b, c, d, e, f, i, x, y)

TickerEntry parse_ticker_entry(std::string const& input) {
    TickerEntry result;

    {
        using namespace boost::spirit::qi;
        using It = std::string::const_iterator;

        rule<It, double()>      qdouble_ = "'" >> double_ >> "'";
        rule<It, std::string()> qstring  = "'" >> *~char_("'") >> "'";

        if (phrase_parse(input.begin(), input.end(),
                    '[' >> qstring >> ','
                    >> qdouble_ >> ',' >> qdouble_ >> ',' >> qdouble_ >> ',' >> qdouble_ >> ',' >> qdouble_ >> ',' >> qdouble_ >> ','
                    >> int_ >> ','
                    >> qdouble_ >> ',' >> qdouble_ >> ']' >> eoi,
                    space, result))
        {
            return result;
        }
    }

    throw std::runtime_error("parse failed");
}

int main() {
    auto ticker = parse_ticker_entry("['BTC_BBR','0.00069501','0.00074346','0.00069501','-0.00742634','8.63286802','11983.47150109',0,'0.00107920','0.00045422']");

    std::cout << ticker.symbol << '\n';
    std::cout << ticker.a << '\n';
    std::cout << ticker.b << '\n';
    std::cout << ticker.c << '\n';
    std::cout << ticker.d << '\n';
    std::cout << ticker.e << '\n';
    std::cout << ticker.i << '\n';
    std::cout << ticker.x << '\n';
    std::cout << ticker.y << '\n';
}

印刷

http://coliru.stacked-crooked.com/a/7696306b81af2f24

BTC_BBR
0.00069501
0.00074346
0.00069501
-0.00742634
8.63287
0
0.0010792
0.00045422
于 2017-08-22T07:48:23.300 回答