I'm using range-v3 by Eric Niebler and using the ranges::views::group_by
function.
In the documentation for group_by
it says: "Given a source range and a binary predicate, return a range of ranges where each range contains contiguous elements from the source range such that the following condition holds: for each element in the range apart from the first, when that element and the first element are passed to the binary predicate, the result is true. In essence, views::group_by groups contiguous elements together with a binary predicate."
So, it returns a range and ranges. My question is how do I access the range of ranges individually? I've tried all the using ways of accessing ranges: p[0].first, p.at(0).first, p.begin.first etc. but no joy.
I have the following code:
#include "https://raw.githubusercontent.com/HowardHinnant/date/master/include/date/date.h"
#include <chrono>
#include <fmt/format.h>
#include <iostream>
#include <map>
#include <range/v3/all.hpp>
using namespace date;
template <typename TimePoint, typename Minutes>
constexpr auto candle_start(const TimePoint timestamp, const Minutes timeframe)
-> TimePoint {
using namespace std::chrono;
const auto time =
hh_mm_ss{floor<minutes>(timestamp - floor<days>(timestamp))};
return timestamp - (time.minutes() % timeframe);
}
auto main() -> int {
{
using namespace std::chrono;
using namespace std::chrono_literals;
const auto tp_now = std::chrono::system_clock::from_time_t(0);
const auto m =
std::map<system_clock::time_point, double>{{tp_now + 0min, 1.1},
{tp_now + 1min, 2.2},
{tp_now + 2min, 3.3},
{tp_now + 3min, 4.4},
{tp_now + 4min, 5.5},
{tp_now + 5min, 6.6},
{tp_now + 6min, 7.7},
{tp_now + 7min, 8.8},
{tp_now + 8min, 9.9},
{tp_now + 9min, 10.10},
{tp_now + 10min, 11.11},
{tp_now + 11min, 12.12},
{tp_now + 12min, 13.13},
{tp_now + 13min, 14.14}};
const auto timeframe = 2min;
const auto same_candle = [timeframe](const auto &lhs, const auto &rhs) {
return candle_start(lhs.first, timeframe) ==
candle_start(rhs.first, timeframe);
};
auto p = m
| ranges::views::reverse
| ranges::views::group_by(same_candle);
// How do I access the range of range values individually?
std::cout << p[0].first << " = " << p[0].second;
return 0;
}
and a play area is given here.