C++ 为什么std::search需要转发ITER

C++ 为什么std::search需要转发ITER,c++,iterator,forward,C++,Iterator,Forward,我的问题与下面的主题相同,我正在努力理解给出的答案,或者更确切地说,我的代码不应该工作,因为它只使用输入迭代器..但是我的func似乎工作起来并且行为与std::search相同..所以我不知所措,不愿意在没有正确理解的情况下继续前进…也许有人可以建议一个会破坏我的函数而不是std:: 发件人: 我正在学习《加速》一书 来自Koenig&Moo的C++ 练习8-2要求我在我的 拥有来自的一些模板化函数 以及 指定我所使用的迭代器类型 实施需要 在尝试实现std::search时, 我决定只需要“

我的问题与下面的主题相同,我正在努力理解给出的答案,或者更确切地说,我的代码不应该工作,因为它只使用输入迭代器..但是我的func似乎工作起来并且行为与std::search相同..所以我不知所措,不愿意在没有正确理解的情况下继续前进…也许有人可以建议一个会破坏我的函数而不是std::

发件人:

我正在学习《加速》一书 来自Koenig&Moo的C++

练习8-2要求我在我的 拥有来自的一些模板化函数 以及 指定我所使用的迭代器类型 实施需要

在尝试实现std::search时, 我决定只需要“输入” 迭代器

然而,从实现的角度来看 与my一起安装的std::search 编译器,我可以看到他们使用 “向前”迭代器,但我不能 明白为什么,因为没有 需要写,只需要读,和输入 迭代器满足要求

这里有人能帮我理解吗 这个,谢谢?为什么我需要使用 要实现的“向前”迭代器 搜索

提前谢谢

我的职能:

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 ;
}
模板
搜索中(开始、结束、开始、结束)
{
In-found;//iter:模式匹配中的第一个元素(在内容中)
在模式中\u begin=begin2;//iter:搜索模式中的第一个元素。
int flag=0;//标志:找到部分匹配?
//搜索模式的内容
while(开始<结束){
//如果模式匹配失败..重置变量
//继续搜索剩余内容/元素(&C)
如果(*开始!=*开始2){
在ret中;
begin2=模式\开始;
flag=0;
begin++;
}否则{
//将模式中的下一个元素与内容中的下一个元素进行比较。
//如果:找到“模式”的第一个元素,则将iter存储到其位置
//…然后,如果找到了整个模式,我们可以将iter重新放置到起始位置
如果(标志==0){
发现=开始;
flag=1;
}
//inc iters比较部分匹配中的下一个元素
begin++;
begin2++;
}
//if:iter是搜索模式的1次结束
//然后找到了整个模式
//将国际热核聚变实验堆返回起始位置
如果(begin2==end2){return found;}
}
//已到达内容结尾,未找到完整的模式
//开始应该?等于iter 1-超过内容结尾
返回开始;
}
司机:

///* // 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 ;

}
//*//驱动程序:自定义::搜索(b、e、b2、e2)
#包括
#包括
#包括
//#包括
#包括“library_algorithms.h”
int main(){
//初始化字符串测试
std::string content=“fo The fox foxu jumped foxe foxy”;
std::string search_pattern=“foxy”;
//字符串的func测试
std::string::迭代器ret_iter=
自定义::搜索(content.begin(),content.end(),search_pattern.begin(),search_pattern.end());
//std::search(content.begin()、content.end()、search_pattern.begin()、search_pattern.end());
//输出
if(ret_iter!=content.end()){

std::cout您误解了输入迭代器的功能

您不能“保存”或复制输入迭代器。它允许您只遍历序列一次。换句话说,这一行将中断:
begin2=pattern\u begin

输入迭代器可能表示您无法轻松“倒带”的内容,例如,从网络适配器接收的数据流。指向“6个元素之前”的迭代器不再有意义,因为该数据可能不再在内存中可用。您只有流中的当前位置


显然,为了正确执行
搜索
,您需要能够多次遍历序列的部分。

这似乎很明显……但myfunction似乎很乐意多次遍历搜索序列..当它找到部分匹配..当模式匹配被破坏时它会重置..直到找到匹配为止一个完整的匹配…sry正在与论坛进行斗争…我将驱动程序添加到我的op中,显示myfunc找到了一个完整的匹配..在多个部分/不完整的匹配结束时..也许如果你能建议一个将破坏我的func的输入..我可以一步一步地做得更好understand@tuk:是,但您正在使用随机访问迭代器调用函数(字符串和向量迭代器)。例如,尝试使用
std::input_iterator
调用它。当然,如果您的代码依赖于前向迭代器上的功能,并且您使用比前向迭代器更强的功能来调用它,那么它就可以正常工作。;)你需要把一个输入迭代器传递给你的函数,看看它是否对输入迭代器工作。())tuk:或者考虑如果输入来自键盘。如果你能回到以前重新键入我输入的内容?它就不见了!这就是为什么我们有一个单独的输入迭代器类别,即单Pas.ER,应该是这样的。当然是std::istream\u迭代器
。或者
std::istream\u迭代器