[英]Complex std::map, structure, std::deque problem
我的目標是有一個有序的 map 時間幀(用於時間序列數據分析),通過向量索引(因為我有時需要按順序引用結構,而不是通過 map 鍵),並引用 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 ready! 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;
};
感謝所有提出建議的人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.