Range-v3:如何将 actions::insert 与地图一起使用

Range-v3: How to use actions::insert with a map

我看过这个 关于如何将 ranges::actions::insert 与矢量一起使用,但我无法让它与地图容器一起使用。

有人可以告诉我在这种情况下如何使用它吗?

我正在尝试使用范围 actions::insert 函数将地图 m 的所有元素插入地图 r。这不是最终结果(我知道还有其他更简洁的方法可以进行复制,但这些方法与我当前的需求无关)但同时向我展示了如何使用 actions::insert 函数。最终结果将类似于:

auto v = m | ranges::views::for_each(return <something conditional true>) | ranges::actions::insert(r, r.end(), <all the element data of m>);

我使用的代码在这里:

#include "https://raw.githubusercontent.com/HowardHinnant/date/master/include/date/date.h"
#include <chrono>
#include <fmt/format.h>
#include <iostream>
#include <limits>
#include <map>
#include <range/v3/all.hpp>

struct pivot_t {
  double price;
  double s;
  unsigned int d;
  std::chrono::system_clock::time_point timestamp;

  pivot_t() : price(0.0), s(0.0), d(0){};
  pivot_t(const double price, const double s, const unsigned int d,
          const std::chrono::system_clock::time_point timestamp)
      : price(price), s(s), d(d), timestamp(timestamp){};
};

auto main() -> int {
  using namespace date;
  using namespace std::chrono;
  using namespace std::chrono_literals;

  // Just used as a reference time point
  const auto tp_now = std::chrono::system_clock::from_time_t(0);

  const auto m = std::map<system_clock::time_point, pivot_t>{
      {tp_now + 0min, {1.1, 1.11, 0, tp_now + 0min}},
      {tp_now + 1min, {2.2, 2.22, 0, tp_now + 1min}},
      {tp_now + 2min, {3.3, 3.33, 0, tp_now + 3min}},
      {tp_now + 3min, {4.4, 4.44, 0, tp_now + 3min}},
      {tp_now + 4min, {3.3, 3.33, 0, tp_now + 4min}},
      {tp_now + 5min, {4.4, 4.44, 0, tp_now + 5min}},
      {tp_now + 6min, {7.7, 7.77, 0, tp_now + 6min}},
      {tp_now + 7min, {8.8, 8.88, 0, tp_now + 7min}},
      {tp_now + 8min, {9.9, 9.99, 0, tp_now + 8min}},
      {tp_now + 9min, {10.10, 10.1010, 0, tp_now + 9min}},
      {tp_now + 10min, {11.11, 11.1111, 0, tp_now + 10min}},
      {tp_now + 11min, {12.12, 12.1212, 0, tp_now + 11min}},
      {tp_now + 12min, {13.13, 13.1313, 0, tp_now + 12min}},
      {tp_now + 13min, {14.14, 14.1414, 0, tp_now + 13min}}};

    auto r = std::map<system_clock::time_point, pivot_t>{};

    auto push_r = [](auto& elem) { return {elem.first, {elem.second.price, elem.second.s, elem.second.d, elem.first}}; };

    // This line effectively makes a copy of m into v. 
    // This is not the final required opertation but it does prove the insert syntax.
    auto v = m | ranges::actions::insert(r, r.end(), push_r);

    for(auto& i : v)
        std::cout << i.first << " = " << i.second.price << std::endl;

    return 0;
}

live demo

我正在使用 lambda,因为这是我能想到的引用单个 m 元素以插入到 r 映射中的唯一方法。如果有另一种(更好的?)方法,请分享。

我收到的错误是这样的:

error: no match for call to '(const ranges::adl_insert_detail::insert_fn) (std::map<std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >, pivot_t>&, std::map<std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >, pivot_t>::iterator, main()::<lambda(auto:28&)>&)'

我想这是在告诉我我的语法不正确,但我不知道如何解决它。

我的objective是按以下方式使用插入函数:

auto v = m | ranges::views::for_each(<selective lambda tbd>) | ranges::actions::insert(r, r.end(), <copy the m (pivot_t complex type) elements to map r (pivot_t complex type) - use a lambda here or other method>)

这里有几处错误。

// This line effectively makes a copy of m into v. 
// This is not the final required opertation but it does prove the insert syntax.
auto v = m | ranges::actions::insert(r, r.end(), push_r);
  1. 您不能通过管道输入 ranges::actions::insert
  2. ranges::actions::insert returns insert 的结果,对于地图来说是 pair<iterator, bool>(而不是可以迭代的某种范围,所以 v 对你没用)
  3. None 中的参数 insert 是可调用的。您可以提供一个值、一个范围、一个 initializer_list、一个 iterator/sentinel 对,或者源范围内的一个位置,以及其中一个。
  4. lambda push_r 本身无效 - 它只是在执行 return { ... } 而没有指定类型的任何地方。你不能只是 return 一个 braced-init-list 这样的,你需要一个类型(return T{...};trailing- return-类型喜欢-> T

I'm trying, using the ranges actions::insert function to insert all the elements of map m into map r

那是:

ranges::actions::insert(r, m);