简体   繁体   English

在自定义容器上使用范围算法

[英]Use ranged algorithm on custom container

I want to upgrade my custom container to compatible with std::ranges algorithms such as find_if and others, like bellow我想升级我的自定义容器以兼容std::ranges算法,例如find_if和其他算法,如下所示

auto is_satisfy = [](CustomContainer::value_type x) { ... };
std::ranges::find_if(custom_container, is_satisfy);
// instead of std::find_if(custom_container.begin(), custom_container.end(), is_satisfy);

Signature of std::ranges::find_if like as std::ranges::find_if的签名就像

struct find_if_fn {
   template< ranges::input_range R,
            class Proj = std::identity,
            std::indirect_unary_predicate<std::projected<ranges::iterator_t<R>,
                                         Proj>> Pred >
   constexpr ranges::borrowed_iterator_t<R>
   operator()( R&& r, Pred pred = {}, Proj proj = {} ) const
   {
       return (*this)(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj));
   }
};

What is the input_range concept and how this feature is supported by my own custom container?什么是input_range 概念以及我自己的自定义容器如何支持此功能?

What is the input_range concept什么是 input_range 概念

It is this:它是这个:

 template<class T> concept input_range = range<T> && input_iterator<iterator_t<T>>;

In English, input_range is a range whose iterator is an input_iterator.在英语中,input_range 是一个迭代器为 input_iterator 的范围。 The range concept on the other hand is:另一方面,范围概念是:

The range concept defines the requirements of a type that allows iteration over its elements by providing an iterator and sentinel that denote the elements of the range.范围概念通过提供表示范围元素的迭代器和哨兵来定义允许对其元素进行迭代的类型的要求。

 template<class T> concept range = requires(T& t) { ranges::begin(t); // sometimes equality-preserving ranges::end(t); };

And input_iterator is: input_iterator 是:

The input_iterator concept defines requirements for a type whose referenced values can be read (from the requirement for indirectly_readable ([iterator.concept.readable])) and which can be both pre- and post-incremented. input_iterator 概念定义了一个类型的要求,该类型的引用值可以被读取(从indirectly_readable ([iterator.concept.readable]) 的要求中)并且可以是前后递增的。 [Note 1: Unlike the Cpp17InputIterator requirements ([input.iterators]), the input_iterator concept does not need equality comparison since iterators are typically compared to sentinels. [注 1:与 Cpp17InputIterator 要求 ([input.iterators]) 不同,input_iterator 概念不需要相等比较,因为迭代器通常与哨兵进行比较。 — end note] ——尾注]

 template<class I> concept input_iterator = input_or_output_iterator<I> && indirectly_readable<I> && requires { typename ITER_CONCEPT(I); } && derived_from<ITER_CONCEPT(I), input_iterator_tag>;

You can go read the specs for the dependee concepts, functions and traits for exact details.您可以阅读 go 的规范,了解依赖概念、功能和特征的详细信息。

how this feature is supported by my own custom container?我自己的自定义容器如何支持此功能?

In short: By conforming to the concepts shown above.简而言之:通过符合上述概念。

In medium: Provide member functions begin and end of which former should return an input iterator and latter should return a compatible sentinel type (which may be same or different from the iterator type).中:提供成员函数的beginend ,前者应返回输入迭代器,后者应返回兼容的标记类型(可能与迭代器类型相同或不同)。 The end sentinel should be reachable from the beginning and represents one past the last element of the range.结束标记应该从一开始就可以到达,并且表示超过范围的最后一个元素。

In general, I recommend looking at API of standard containers to find out what members they provide and how they work.一般来说,我建议查看标准容器的 API 以了解它们提供哪些成员以及它们是如何工作的。 Copy the design to your custom containers.将设计复制到您的自定义容器中。


Quotes are from the latest standard draft.报价来自最新的标准草案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM