【问题标题】:why does std::search need forward iters为什么 std::search 需要转发迭代
【发布时间】:2011-07-19 02:45:54
【问题描述】:

我的问题与下面的线程相同,我很难理解给出的答案,或者我的代码不应该工作,因为它只使用输入迭代器..但我的 func 似乎工作并且与 std 的行为相同: :search ..所以我很茫然,不愿意在没有正确理解的情况下继续前进...也许如果有人可以提出一个会破坏我的功能而不是 std::

的输入

来自Why do I need a Forward Iterator to implement my customized std::search

我正在研究《加速》这本书 C++”,来自 Koenig & Moo。

练习 8-2 要求我在我的 拥有一些模板化的函数 ,以及 指定我的迭代器类型 实施要求。

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

但是,查看实现 与我一起安装的 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 ;
}

司机:

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

}

【问题讨论】:

    标签: c++ iterator forward


    【解决方案1】:

    您误解了输入迭代器的功能。

    您不能“保存”或复制输入迭代器。它允许您只遍历序列一次。换句话说,这条线,除其他外,将中断:begin2 = pattern_begin

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

    很明显,为了正确实现search,您需要能够多次遍历序列的各个部分。

    【讨论】:

    • 这似乎很明显......但是 myfunction 似乎很乐意多次遍历搜索序列......当它找到部分匹配时......它在模式匹配被破坏时重置......直到它找到一场完整的比赛......
    • sry 在论坛上苦苦挣扎....我已将驱动程序添加到我的操作中,显示 myfunc 找到完整匹配..在多个部分/不完整匹配的末尾...也许如果你可以提出一个会破坏我的功能的输入。我可以逐步了解
    • @tuk:是的,但是您正在使用随机访问迭代器(字符串和向量迭代器)调用该函数。例如,尝试使用std::input_iterator 调用它。当然,如果您的代码依赖于前向迭代器上的功能,并且您使用比前向迭代器更强 的东西来调用它,它会正常工作。 ;) 您需要将输入迭代器实际传递给您的函数,以查看它是否适用于输入迭代器。 :)
    • @tuk:或者考虑一下如果输入来自键盘会发生什么。你怎么能回去重读我之前输入的内容?它消失了!这就是为什么我们有一个单独的 input_iterator 类别,即单通道。
    • 呃,当然应该是std::istream_iterator。或std::istreambuf_iterator.
    猜你喜欢
    • 2017-02-19
    • 1970-01-01
    • 1970-01-01
    • 2017-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多