简体   繁体   English

成为`stl`算法兼容容器需要什么?

[英]What does it take to be an `stl` algorithm compatible container?

1) For a given stl algorithm, how does one find out what custom container/iterator features one needs to implement in order to use said container? 1)对于给定的stl算法,如何找出为了使用所述容器需要实现的自定义容器/迭代器功能?

2) What needs to be implemented to make container fully compatible with all stl algorithms? 2)需要实现什么才能使容器与所有stl算法完全兼容?

In C++11 standard it is stated in §25.1, in the footnotes: 在C ++ 11标准中,脚注中的§25.1中对此进行了说明:

3 All of the algorithms are separated from the particular implementations of data structures and are parameterized by iterator types. 3所有算法都与数据结构的特定实现分开,并由迭代器类型进行参数化。 Because of this, they can work with program-defined data structures, as long as these data structures have iterator types satisfying the assumptions on the algorithms. 因此,只要这些数据结构具有满足算法假设的迭代器类型它们就可以使用程序定义的数据结构。

and then: 然后:

5 Throughout this Clause, the names of template parameters are used to express type requirements. 5在整个条款中,模板参数的名称用于表示类型要求。 If an algorithm's template parameter is InputIterator, InputIterator1, or InputIterator2, the actual template argument shall satisfy the requirements of an input iterator (24.2.3). 如果算法的模板参数是InputIterator,InputIterator1或InputIterator2,则实际模板参数应满足输入迭代器 (24.2.3)的要求。 If an algorithm's template parameter is OutputIterator, OutputIterator1, or OutputIterator2, the actual template argument shall satisfy the requirements of an output iterator (§24.2.4). 如果算法的模板参数是OutputIterator,OutputIterator1或OutputIterator2,则实际模板参数应满足输出迭代器的要求 (第24.2.4节)。 If an algorithm's template parameter is ForwardIterator, ForwardIterator1, or ForwardIterator2, the actual template argument shall satisfy the requirements of a forward iterator (§24.2.5). 如果算法的模板参数是ForwardIterator,ForwardIterator1或ForwardIterator2,则实际模板参数应满足前向迭代器的要求 (第24.2.5节)。 If an algorithm's template parameter is BidirectionalIterator, Bidirectional-Iterator1, or BidirectionalIterator2, the actual template argument shall satisfy the requirements of a bidirectional iterator (§24.2.6). 如果算法的模板参数是BidirectionalIterator,Bidirectional-Iterator1或BidirectionalIterator2,则实际模板参数应满足双向迭代器的要求 (第24.2.6节)。 If an algorithm's template parameter is RandomAccessIterator, Random- AccessIterator1, or RandomAccessIterator2, the actual template argument shall satisfy the requirements of a random-access iterator (§24.2.7). 如果算法的模板参数是RandomAccessIterator,Random- AccessIterator1或RandomAccessIterator2,则实际模板参数应满足随机访问迭代器的要求 (第24.2.7节)。

So basically you need to provide your custom class a way to retrieve the usual iterators and these object must satisfy the requirement depending on which algorithm you want to support. 所以基本上你需要为自定义类提供一种检索常用迭代器的方法,这些对象必须满足要求,具体取决于你想要支持的算法。

For example, let's take std::any_of , you can see it's declared as 例如,让我们拿std::any_of ,你可以看到它被声明为

template <class InputIterator, class Predicate> 
bool any_of(InputIterator first, InputIterator last, Predicate pred);

So you need to provide it an input iterator, then you can see at §24.2.3 the requirement for it and implement them: 所以你需要为它提供一个输入迭代器,然后你可以在§24.2.3中看到它的要求并实现它们:

  • contextually convertible to bool ( a != b ) 上下文可转换为bool( a != b
  • convertible to T ( *a ) 可转换为T( *a
  • dereferenceable ( a->m ) 可解除引用的( a->m
  • incrementable 递增的

Same story for each kind of iterator. 每种迭代器都有相同的故事。

STL algorithms use template types to describe the requirements of the function. STL算法使用模板类型来描述函数的要求。 If you take std::sort as an example 如果你以std::sort为例

template<class RandomAccessIterator>
void sort(RandomAccessIterator first, RandomAccessIterator last);

We can see that wee need to pass a random access compliant iterator. 我们可以看到,我们需要传递一个随机访问兼容的迭代器。 So as long as the container you have has an iterator type of RandomAccessIterator or it supports all of the operations that RandomAccessIterator supports then you can use it with std::sort . 因此,只要您拥有的容器具有迭代器类型的RandomAccessIterator,或者它支持RandomAccessIterator支持的所有操作,那么您可以将它与std::sort

If we look at the iterator hierarchy on cppreference We can see that in the current standard a RandomAccessIterator is a super-set of all other iterator types and supports all of the operations that the other iterators support. 如果我们查看cppreference上的迭代器层次结构我们可以看到,在当前标准中, RandomAccessIterator是所有其他迭代器类型的超集,并支持其他迭代器支持的所有操作。 So if a function calls for a InputIterator we can still give it an RandomAccessIterator. 因此,如果函数调用InputIterator,我们仍然可以给它一个RandomAccessIterator。

So right now if your container has a RandomAccessIterator you can use it in any of the STL algorithms. 所以现在如果您的容器有一个RandomAccessIterator,您可以在任何STL算法中使用它。

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

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