c++ - 复杂的 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
我需要做什么来实现我的目标并解决编译错误?
谢谢。
解决方案
有人帮助了我,非常感谢他们。
解决方法如下:
#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;
};
感谢所有提出建议的人。
推荐阅读
- python - 为什么 concurrent.futures.ThreadPoolExecutor().submit 不立即返回?
- webpack - Webpack 开发服务器缓存清除
- javascript - 仅操作二维数组的数值
- java - 在java中向服务器发送消息
- javascript - Newsletter2Go - 用 javascript 填写表单后无法正常工作?
- java - 我试图在 Java 中的一串连续字母中找到丢失的字母
- azure - datediff 上的 Azure 流分析加入未按预期工作
- java - 读取托管对象时使用别名注释
- ms-access - 在 MS Access 数据宏中将字符串转换为日期
- ios - 在 StackView 中按内容对齐两个元素