使用 c++11 构造映射函数

Map function with c++11 constructs

本文关键字:映射函数 c++11 使用      更新时间:2023-10-16

既要自学实现比简单的基本模板结构更高级的模板结构,又要使它们在许多情况下很有用,我正在尝试使用 c++11 结构(如 decltype(实现函数式编程中常见的映射、过滤器和类似函数。

我在创建我使用的编译器可以处理的函数原型时遇到问题,所以我不得不问你如何创建这样的东西:

//
// Takes an iterable, applies a function to every element, and returns a vector of the results
//
template <typename T, typename Func>
auto map(const T& iterable, Func func) -> std::vector< decltype(  func( *iterable.cbegin() ) ) >
{
    // body snipped
}

也就是说,此函数应接受任何可迭代对象,以及将可迭代对象值类型作为参数并返回某种值的函数。函数调用的结果将是一个向量,无论传入的可迭代对象类型如何,都是传递的函数返回的类型。

map 函数应接受任何具有有效原型的函数作为参数,无论是函数指针、函子还是 lambda 表达式。

将上面的函数与此测试代码一起使用:

std::vector<int> intVector;
intVector.push_back(1);
intVector.push_back(2);
map(intVector, [](int& value) { return value + 1; });

使Visual Studio吐出C2893("无法专用函数模板"(错误,我不确定出了什么问题。

更新:到目前为止,在评论和回答中建议的应用更改,测试了新原型,但仍然存在相同的错误。

这可能会做你想要的。它在内部使用std::transform,基本上完成了整个工作。我编写的函数只不过是容器的简单包装器(不适用于 C 样式数组,这需要一些额外的类型特征(:

#include <vector>
#include <algorithm>
#include <type_traits>
//
// Takes an iterable, applies a function to every element, 
// and returns a vector of the results
//
template <typename T, typename Func>
auto map_container(const T& iterable, Func&& func) ->
    std::vector<decltype(func(std::declval<typename T::value_type>()))>
{
    // Some convenience type definitions
    typedef decltype(func(std::declval<typename T::value_type>())) value_type;
    typedef std::vector<value_type> result_type;
    // Prepares an output vector of the appropriate size
    result_type res(iterable.size());
    // Let std::transform apply `func` to all elements
    // (use perfect forwarding for the function object)
    std::transform(
        begin(iterable), end(iterable), res.begin(),
        std::forward<Func>(func)
        );
    return res;
}

但是,请注意,您的 lambda 应该引用 const ,或者在 int 的情况下最好按值引用其参数。

另外,我将函数从map重命名为map_container:在程序中为函数、变量或其他任何内容重用C++标准库的标准容器的名称是一种糟糕的编程实践。

对我来说,这给出了所需的输出:

#include <iostream>
int main()
{
    std::vector<int> intVector;
    intVector.push_back(1);
    intVector.push_back(2);
    auto v = map_container(intVector, [] (int value) { return value + 1; });
    for (int i : v) { std::cout << i << " "; }
}

所以这里有一大堆极端情况需要处理。 我要做的是首先尝试构建一些container_traits模板来抽象尽可能多的工作。

如果类型允许调用beginend自由函数,其中std::beginstd::end已通过using发挥作用,并且这两种类型是相同的(最后一个可能不是必需的(,则该类型是container

container的特征主要来自容器具有的iterator,以及所述迭代器的类型。 其他一些功能,如size(甚至size_at_least - 见下文(,很常见。

如果类型constcontainer,则称该类型iterable

下一个问题是"什么样的类型实例对于映射容器的元素是有效的?"——这也有点不平凡,所以我添加了一些特征类来处理它。

因此,这导致了以下实现:

#include <algorithm>
#include <type_traits>
#include <utility>
namespace aux {
  // calculate the type that calling `begin` and `end` on a type will return
  // in a scope where `std::begin` and `std::end` are visible.  This hack is
  // required to enable argument-dependent lookup.
  using std::begin;
  using std::end;
  template<typename T>
  auto adl_begin(T&&t)->decltype( begin(std::forward<T>(t)) );
  template<typename T>
  auto adl_end(T&&t)->decltype( end(std::forward<T>(t)) );
  template<typename T>
  auto adl_cbegin(T const&t)->decltype( begin(t) );
  template<typename T>
  auto adl_cend(T const&t)->decltype( end(t) );
}
// What is a container?  Something with a `begin`ing and an `end`ing...
template<typename C,typename=void>
struct is_container:std::false_type {};
template<typename C>
struct is_container<C, typename std::enable_if<
   std::is_same<
      decltype(aux::adl_begin(std::declval<C>())),
      decltype(aux::adl_end(std::declval<C>()))
   >::value
>::type >:std::true_type {};

// Default container_traits is empty for SFINAE ease of use:
template<typename C, typename=void>
struct container_traits {};
// if it is a container, go in whole hog:
template<typename C>
struct container_traits<C, typename std::enable_if< is_container<C>::value >::type >
{
   typedef decltype( aux::adl_begin(std::declval<C>()) ) iterator;
   typedef decltype( aux::adl_cbegin(std::declval<C>()) ) const_iterator;
   // I'm lazy, so I'll copy typedefs from `iterator_traits` below:
   typedef typename std::iterator_traits<iterator>::value_type value_type;
   typedef typename std::iterator_traits<iterator>::reference reference;
   // etc
   // TODO: size_at_least is a helper function
   // it returns 0 if it is expensive to calculate the size (say, a range
   // if iterators into a `std::list`), and the size if it is cheap to
   // calculate (say, a `std::vector`, any class with a `.size()` method,
   // or a pair of pointers or other random-access iterators)
   // template<typename C2, typename=typename std::enable_if< std::is_convertable< C2, C const&>::value>::type
   // static std::size_t size_at_least( C2&& c ) { ... }
};
// Can Functor map the elements of C into something we can store elsewhere?
template<typename C, typename Functor, typename=void>
struct can_map:std::false_type {};
// Yes, if the result of calling Functor on C's elements is non-void:
template<typename C, typename Functor>
struct can_map<C, Functor, typename std::enable_if<
  !std::is_same< decltype(std::declval<Functor>()(std::declval<typename container_traits<C>::value_type>())), void >::value
>::type>: std::true_type {};
// The result of mapping the elements of C under Functor
template<typename C, typename Functor, typename=void>
struct map_result {};
template<typename C, typename Functor>
struct map_result<C,Functor,typename std::enable_if< can_map<C,Functor>::value>::type>
{
  typedef
    decltype(
      std::declval<Functor>()(
        *std::declval<
          typename container_traits<C>::const_iterator
        >()
      )
    )
  type;
};
// The actual implementation
// we std::decay the map_result because we want to store
// instances of the type, and std::decay does that quite nicely
// note that some pathological Functors may break this, ie ones
// that return pseudo-references that are intended to be read from
// yet are not std-container safe
template <typename T, typename Func>
auto map_container(T&& iterable, Func&& func) ->
  std::vector<
    typename std::decay<
      typename map_result<T, Func>::type
    >::type
  >
{
  std::vector<
    typename std::decay<
      typename map_result<T, Func>::type
    >::type
  > retval;
  // TODO: use container_traits<T>::size_at_least to reserve space in retval
  // that will bring the efficiency of this function up to near-hand-crafted-C.
  for (auto&& s:iterable) {
    retval.push_back( func(s) );
  }
  return retval;
}

仅此而已。 接下来,测试代码。 我们应该能够通过 .begin() 方法和自由浮动begin(C)函数vector C 样式数组、传统类型和bool(使用伪引用并紧密打包位(以及用户定义的类型进行map_container

我在数组中遇到的一个问题是C const&似乎会导致数组中的指针衰减,这使得它不再是一个容器:我必须绑定到C&&才能获得真正的数组类型。

#include <iostream>
void test1() {
   std::vector<int> src{1,2,3,4,5};
   auto r = map_container( src, [](int x){return x*2;});
   for (auto&& x:r) {
      std::cout << x << "n";
   }
}
struct test_buffer {
  int foo[5];
  int* begin() { return foo; }
  int* end() { return &foo[5]; }
  int const* begin() const { return foo; }
  int const* end() const { return &foo[5]; }
};
test_buffer buff1={{1,2,3,4,5}};
struct test_buffer_2 {
  int foo[5];
};
test_buffer_2 buff2={{1,2,3,4,5}};
int* begin(test_buffer_2& t) { return t.foo; }
int* end(test_buffer_2& t) { return &t.foo[5]; }
int const* begin(test_buffer_2 const& t) { return t.foo; }
int const* end(test_buffer_2 const& t) { return &t.foo[5]; }
std::vector<bool> bits{true, false, true, false};   
template<typename Container>
void tester(Container&& c) {
   Container const& src = c;
   auto r = map_container( src, [](int x){return x*2;});
   for (auto&& x:r) {
      std::cout << x << "n";
   }
}
void test2() {
   tester(buff1);
   tester(buff2);
   tester(bits);
}
template<typename C>
bool is_container_test(C&&) {
   return is_container<C>::value;
}
template<typename C, typename F>
bool can_map_test( C&&, F&& ) {
   return can_map<C, F>::value;
}
template<typename C, typename F>
bool can_map_test2( C const&, F&& ) {
   return can_map<C, F>::value;
}
int array[] = {1,2,3,4,5};
void test3() {
   std::cout << "Array is container:" << is_container_test(array) << "n";
   auto x2 = [](int x){return x*2;};
   std::cout << "Double can map:" << can_map_test(array, x2) << "n";
   std::cout << "Double can map:" << can_map_test2(array, x2) << "n";
}
void test4() {
   tester(array);
}
int main() {
   test1();
   test2();
   test3();
   test4();
}

或类似的东西。 不要在函数本身中执行复杂的 SFINAE,而是创建为您完成工作的特征类。

上面使用的其他技术:我使用std::beginstd::end来获取开始/结束迭代器。 这意味着我现在支持原始 C 数组。 然后,我将其包装在一些依赖于参数的查找帮助程序中,其目的是允许您在同一命名空间中使用类覆盖定义beginend

请注意,container_traits的"不接受"版本是一个空结构,而不是未定义的结构。 这使我们能够在其他地方使用SFINAE中的container_traits

哦,效率改进是编写"智能储备",它采用具有reserve方法的容器和您希望复制其大小的容器。 如果要复制的容器缺少随机访问迭代器并且缺少.size()方法,则它不执行任何操作,但如果这样做,则会执行.reserve( end(...)-begin(...) ).reserve(src.size())。 我们可以将其抽象为其他算法,将其作为static size_t size_at_least(Container const&)添加到container_traits中,这将返回一个在 O(1( 时间内不大于Container大小的size_t

对Andy Prowl上面精彩回答的一些小改进:

  1. 我们可以提前保留我们需要的大小,而无需实际调整向量的大小(从而避免默认构造 n 个元素(。这有两个优点:
    1. 它允许您映射根本不默认构造的类型
    2. 它可能具有显着的性能优势(因为编译器很少能够省略那些未使用的默认结构(,具体取决于元素的数量以及它们的构造成本。
  2. 我们可以提供适用于迭代器范围的重载,以便您可以在容器的一部分上进行操作。

您可以在编译器资源管理器中使用它,但代码本身如下所示:

template <typename Iterator, typename Func> [[nodiscard]]
auto functional_map(Iterator begin, Iterator end, Func && func) ->
std::vector<decltype(func(std::declval<typename Iterator::value_type>()))>
{
    using value_type = decltype(func(std::declval<typename Iterator::value_type>()));
    std::vector<value_type> out_vector;
    out_vector.reserve(std::distance(begin, end));
    
    std::transform(begin, end, std::back_inserter(out_vector),
                   std::forward<Func>(func));
    
    return out_vector;
}
template <typename T, typename Func> [[nodiscard]]
auto functional_map(const T & iterable, Func && func) ->
std::vector<decltype(func(std::declval<typename T::value_type>()))>
{
    return functional_map(std::begin(iterable), std::end(iterable),
                          std::forward<Func>(func));
}

(请注意,我在这些声明中使用了 C++17 的 [[nodiscard]] 属性,但如果您在 C++17 之前,您可以毫无问题地删除它。

编译器资源管理器链接还包括几个演示性测试:

TEST_CASE("Mapping ints to string")
{
    const std::vector<int> int_version{0, 1, 2, 3, 4};
    const std::vector<std::string> string_version{"0", "1", "2", "3", "4"};
    CHECK(functional_map(int_version, 
                         [](int i) { return std::to_string(i); }) == string_version);
    
    CHECK(functional_map(string_version, 
                         [](const std::string & s) { return std::stoi(s); }) == int_version);
}
TEST_CASE("Mapping over only part of a container")
{
    const std::vector<int> int_version{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    const std::vector<std::string> string_version{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
    const std::vector<std::string> first_four_strings(string_version.begin(), string_version.begin() + 4);
    CHECK(functional_map(int_version.begin(), int_version.begin() + 4, 
                         [](int i) { return std::to_string(i); }) == first_four_strings);
    
    const std::vector<int> first_four_ints(int_version.begin(), int_version.begin() + 4);
    CHECK(functional_map(string_version.begin(), string_version.begin() + 4, 
                         [](const std::string & s) { return std::stoi(s); }) == first_four_ints);
}