首页 > 解决方案 > 复杂的 std::map、结构、std::deque 问题

问题描述

我的目标是有一个时间帧的有序映射(用于时间序列数据分析),通过向量索引(因为我有时需要按顺序引用结构而不是通过映射键),并引用 OHLCCandle 结构并得到结果通过双端队列呈现的数据。

数据访问将类似于:data_[5].get().open[0]意味着访问 5 分钟时间范围以在班次 0 检索打开的数据,或者作为另一个示例,data_[15].get().close[4]意味着访问 15 分钟时间范围以在班次 4 检索关闭数据(0 是最新的)。

到目前为止,我的代码是:

#include <deque>
#include <vector>
#include <map>
#include <iostream>

template<typename Price>
struct OHLCCandle final
{
    static_assert(std::is_floating_point<Price>::value, "");

    using Container = std::deque<Price>;

    Container open;
    Container high;
    Container low;
    Container close;
};

using Candle = OHLCCandle<double>;
using CandleContainer = std::vector<Candle>;
using Timeframe = int;

std::map<Timeframe, std::reference_wrapper<Candle>> data_;

auto emplace(const int timeframe, const Candle& candle) -> decltype(auto)
{
    auto error = int{ 0 };
    try
    {
        //////// Problem area begin
        auto candles = CandleContainer{};
        candles.push_back(candle);

        const auto pair = data_.emplace(std::make_pair(timeframe, candles));
        //////// Problem area end

        if (!pair.second)
        {
            std::cout << "ERROR: Emplacement failed, doing nothing. timeframe = " << timeframe << std::endl;
            error = (std::numeric_limits<int>::min)();
        }
    }
    catch (const std::exception& e) {
        std::cout << "ERROR: Unknown error occured, ignoring. timeframe = " << timeframe << ", erc = " << e.what() << std::endl;
        error = (std::numeric_limits<int>::min)();
    }

    return error;
};

但是,我不确定如何构造 CandleContainer 结构,以便它可以与std::map代码的一部分相结合,并且我在 godbolt.org 下收到以下错误(代码是 godbolt.org 准备好的!- https://godbolt.org/z/d9HByG):

[x64 msvc v19.22 #1] note: see reference to function template instantiation 'std::pair<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,bool> std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::emplace<std::pair<int,std::vector<Candle,std::allocator<OHLCCandle<double>>>>>(std::pair<int,std::vector<Candle,std::allocator<OHLCCandle<double>>>> &&)' being compiled
[x64 msvc v19.22 #1] note: see reference to function template instantiation 'std::pair<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,bool> std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::emplace<std::pair<int,std::vector<Candle,std::allocator<OHLCCandle<double>>>>>(std::pair<int,std::vector<Candle,std::allocator<OHLCCandle<double>>>> &&)' being compiled

我需要做什么来实现我的目标并解决编译错误?

谢谢。

标签: c++c++14

解决方案


有人帮助了我,非常感谢他们。

解决方法如下:

#include <deque>
#include <functional>
#include <vector>
#include <map>
#include <iostream>
#include <limits>

template<typename Price>
struct OHLCCandle final
{
    static_assert(std::is_floating_point<Price>::value, "");

    using Container = std::deque<Price>;

    Container open;
    Container high;
    Container low;
    Container close;
};

using Candle = OHLCCandle<double>;
using CandleContainer = std::vector<Candle>;
using Timeframe = int;

std::map<Timeframe, std::reference_wrapper<Candle>> data_;
CandleContainer candles_;

auto emplace(const int timeframe, const Candle& candle) -> decltype(auto)
{
    auto error = int{ 0 };
    try
    {
        candles_.push_back(candle);
        const auto pair = data_.emplace(std::make_pair(timeframe, std::ref(candles_.back())));

        if (!pair.second)
        {
            std::cout << "ERROR: Emplacement failed, doing nothing. timeframe = " << timeframe << std::endl;
            error = (std::numeric_limits<int>::min)();
        }
    }
    catch (const std::exception& e) {
        std::cout << "ERROR: Unknown error occured, ignoring. timeframe = " << timeframe << ", erc = " << e.what() << std::endl;
        error = (std::numeric_limits<int>::min)();
    }

    return error;
};

感谢所有提出建议的人。


推荐阅读