簡體   English   中英

為什么std :: search需要轉發iters

[英]why does std::search need forward iters

我的問題與下面的線程相同,我很難理解給出的答案,或者說我的代碼不應該工作,因為它只使用輸入迭代器..但我的func似乎工作和行為與std :: search相同。 。所以我很茫然,不願意繼續前進而不理解......也許如果有人可以提出一個會破壞我的功能而不是std的輸入::

我為什么需要Forward Iterator來實現我自定義的std :: search

我正在研究Koenig&Moo的“Accelerated C ++”一書。

練習8-2讓我自己實現一些來自<algorithm>和<numeric>的模板化函數,並指定我的實現需要什么樣的迭代器。

在嘗試實現std :: search時,我確定我只需要“輸入”迭代器。

但是,看看用我的編譯器安裝的std :: search的實現,我可以看到它們使用“前向”迭代器,但是我無法理解為什么,因為沒有必要編寫,只能讀取,輸入迭代器符合需求。

請問有人幫我理解這個嗎? 為什么我需要使用“前向”迭代器來實現std :: search?

提前致謝。

MyFunction的:

template <class In> 
In search(  In begin, In end, In begin2, In end2 )
{
    In found ;                      // iter: 1st element in pattern match(in content)
    In pattern_begin = begin2 ;     // iter: 1st element in search pattern.
    int flag = 0 ;                  // flag: partial match found?

    // search content for pattern 
    while (  begin < end  ) {

        // if pattern-match fails ..reset vars
        // & continue searching remaining content/elements
        if ( *begin != *begin2 ) {

            In ret ;                     
            begin2 = pattern_begin ;
            flag = 0 ;
            begin++ ;


        } else {
            // compare next element in pattern with next element in content.
            // if: 1st element of 'pattern' is found, store iter to it's pos
            // ..then if entire pattern is found, we can ret an iter to where it starts
            if ( flag == 0 ) { 
                found = begin ;
                flag = 1 ;
            }
            // inc iters to compare next elements in partial match
            begin++ ;
            begin2++ ;
        }

        // if: iter is 1-past end of search pattern
        // then entire pattern has been found 
        // return the iter to where it starts
        if( begin2 == end2 ) {  return found ;  }

    }

    // end of content reached, no complete pattern found
    // begin should? equal an iter 1-past the end of content
    return begin ;
}

司機:

///* // Driver: custom::search(  b, e, b2, e2  ) 
#include <string>
#include <vector>
#include <iostream>
//#include <algorithm>
#include "library_algorithms.h"

int main() {

    // init string test
    std::string content = "fo The fox  foxu jumped  foxe foxy " ;
    std::string search_pattern = "foxy" ;

    // func test on string
    std::string::iterator ret_iter = 
    custom::search(  content.begin(), content.end(), search_pattern.begin(), search_pattern.end()  ) ;
    //std::search(  content.begin(), content.end(), search_pattern.begin(), search_pattern.end()  ) ;

    // output
    if (  ret_iter != content.end()  ) {

        std::cout << std::endl << std::endl << search_pattern << ": found at position: " << int(  ret_iter - content.begin()  ) << std::endl;

    } else { 

        std::cout << std::endl << std::endl << search_pattern << ": ...not found" << std::endl;
    }




    // Init vec test:
    // create content values in range:  10 20 30 <......> 9970 9980 9990
    std::vector<int> myvector;
    for (int i=1; i<1000; i++) myvector.push_back(i*10);

    // create pattern values to search for
    std::vector<int> pattern ;
    pattern.push_back( 3730 ) ;
    pattern.push_back( 3740 ) ;
    pattern.push_back( 3750 ) ;
    pattern.push_back( 3760 ) ;

    // test: func on vector<int>
    std::vector<int>::iterator it;
    it = custom::search (  myvector.begin(), myvector.end(), pattern.begin(), pattern.end() );

    // output
    if (it!=myvector.end())
    std::cout << std::endl << std::endl << "pattern found at position " << int(it-myvector.begin()) << std::endl;
    else
    std::cout << std::endl << std::endl << "pattern not found" << std::endl;





    return 0 ;

}

你誤解了輸入迭代器可以做什么。

您不能“保存”或復制輸入迭代器。 它允許您只掃描序列一次。 換句話說,這一行將會破壞: begin2 = pattern_begin

輸入迭代器可能代表您無法“快退”的內容,例如,從網絡適配器接收的數據流。 指向“6個元素之前”的迭代器不再有意義,因為該數據可能在內存中不再可用。 您只有流中的當前位置。

顯而易見的是,為了正確實現search ,您需要能夠多次遍歷序列的某些部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM