gpt4 book ai didi

c++ - 复杂的std::map、结构、std::deque问题

转载 作者:太空宇宙 更新时间:2023-11-04 12:32:15 26 4
gpt4 key购买 nike

我的目标是有一个有序的时间帧图(用于时间序列数据分析),通过 vector 索引(因为我有时需要按顺序引用结构而不是通过映射键),并引用 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

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

谢谢。

最佳答案

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

解决方法如下:

#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;
};

感谢所有提出建议的人。

关于c++ - 复杂的std::map、结构、std::deque问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58202682/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com