简体   繁体   English

第2部分:为什么我需要一个正向迭代器来实现我的自定义std :: search

[英]PART 2 : Why do I need a Forward Iterator to implement my customized std::search

Don't think its necessary to read part one, but I'l include the link just incase: why does std::search need forward iters 不要认为阅读第一部分是必要的,但是我只是以防万一:我为什么要包含链接: 为什么std :: search需要转发迭代器

.....almost there with iterator catagories(I think) ..I looked around to find an easy all-in-1 table, that shows the functionality available from the various types of iterators ..couldn't find one so I'v tried to expand stroustrup's table to include things like: the ability to pass over a range more than once etc...let me know if I'v missed or misunderstood anything? .....几乎有迭代器类别(我认为)..我环顾四周,找到一个简单的全合一表,该表显示了各种类型的迭代器可用的功能..找不到,所以我'v试图扩展stroustrup的表格,使其包含以下内容:能够多次通过范围等等……让我知道我是否错过或误解了什么? ..or if there's a better table kicking about ..或者还有更好的餐桌

*1++ must be (de)referenced between incrementions * 1 ++必须在增量之间(取消)引用

*n++ can be incremented more than once without being (de)referenced * n ++可以多次被递增,而无需被(取消)引用

*n_save range can be passed over more than once and also saved/copied * n_save范围可以多次传递,也可以保存/复制

------------------------------------------------------------------------------
-  Iterator Operations and Categories                                     
------------------------------------------------------------------------------
Category:        output  input    forward    bidirectional   random-access
Abbreviation:    Out     In       For        Bi              Ran 
------------------------------------------------------------------------------
Read(*1++):              =*p            
Read(*n++):                       =*p        =*p             =*p
Read(*n_save):                    =*p        =*p             =*p

Write(*1++):     *p=             
Write(*n++):                      *p=        *p=             *p=
Write(*n_save):                   *p=        *p=             *p=

Access:                   ->      ->         ->              ->[]

Iteration:        ++      ++      ++         ++--            ++ -- + - += -=
Comparison:               == !=   == !=      == !=           == != < > >= <= 
------------------------------------------------------------------------------

Write(*n_save) ...wasn't sure if copying/saving an iter is read or write ..so I added it to both? Write(* n_save) ...不确定是复制还是保存iter是读还是写..所以我将其添加到两者中? ..Im guessing if you can read-pass a range more than once..you might also want to write-pass a range more than once? ..我在猜测是否可以多次通过传递范围..您可能还想多次传递通过范围?

I now understand why std::search needs forward iterators, but unsure why it needs 4 ..would 2 For & 2 In suffice? 我现在知道为什么std :: search需要转发迭代器,但是不确定为什么需要4 ..would 2 For&2 In足够了吗?

while (  begin != end  ) {     
if( begin2 == end2 ) {  return found ;  }
}

..is it because end and end2 are refd more than once( every time the while loops)..? ..是因为endend2被多次引用了(每次while循环)。

template <class For, class In> 
For search(  For begin, In end, For begin2, In end2 )
{
    For found ;                     
    For pattern_begin = begin2 ;    //refd
    int flag = 0 ;                  

    // search content for pattern 
    while (  begin != end  ) {      //refd

        if ( *begin != *begin2 ) {    //de-refd

            begin2 = pattern_begin ;  //store/copy
            flag = 0 ;
            begin++ ;             //inc


        } else {

            if ( flag == 0 ) { 

                found = begin ;
                flag = 1 ;
            }

            begin++ ;
            begin2++ ;
        }

        if( begin2 == end2 ) {  return found ;  } //refd

    }

    return begin ;
}

I think you've pulled the "must be incremented between dereferencing" out of thin air. 我认为您已经摆脱了“在取消引用之间必须增加”的问题。

Input and output operator implement those, so that they could be used by a function that doesn't expect them in particular, but essentially incrementing may well be a no-op with them. 输入和输出运算符实现了这些功能,因此它们可以被不期望它们使用的功能使用,但是本质上增加可能对它们不起作用。

That search function needs four iterators, because otherwise there would simply be no way to tell where either range ends. 该搜索功能需要四个迭代器,因为否则将根本无法确定两个范围的结束位置。 An iterator by itself does not (necessarily) know whether it is at the end of the range or not. 迭代器本身不(不必要)不知道它是否在范围的末尾。

A range in SC++L is represented by a pair of iterators of the same type. SC ++ L中的范围由一对相同类型的迭代器表示。 Technically the algorithms could accept iterators of different type for a single range, but that would hardly have any practical use and just make the code more error-prone. 从技术上讲,算法可以在单个范围内接受不同类型的迭代器,但是几乎没有任何实际用途,只会使代码更容易出错。 As it is, at least one kind of errors can be detected at compile-time: 实际上,在编译时可以检测到至少一种错误:

void foo(container& a, const container& b, const container& c) {
    std::search(a.begin(), b.end(), c.begin(), c.end());
}

The error here is passing iterators into different containers for the first two arguments. 这里的错误是将迭代器传递给前两个参数的不同容器。 But in this case this would be caught at compile-time, because fortunately a and b happen to have different constness, so a.begin() returns container::iterator and b.end() returns a different type container::const_iterator . 但是在这种情况下,这将在编译时被捕获,因为幸运的ab恰好具有不同的a.begin() ,因此a.begin()返回container::iteratorb.end()返回不同类型的container::const_iterator If all four arguments were allowed to be of different types, then this error will lead to undefined behavior at runtime. 如果允许所有四个参数均为不同类型,则此错误将导致运行时未定义的行为。

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

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