为什么 std::search 需要前向迭代

2024-04-04

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

From 为什么我需要一个正向迭代器来实现我的自定义 std::search https://stackoverflow.com/questions/3795114/why-do-i-need-a-forward-iterator-to-implement-my-customized-stdsearch :

我正在学习《加速》这本书 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:

///* // 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 个元素之前”的迭代器不再有意义,因为该数据可能不再在内存中可用。你只有current在流中的位置。

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么 std::search 需要前向迭代 的相关文章

随机推荐

  • 类型“JQuery”上不存在属性“slick”

    我有一个想要使用的 Angular 6 项目光滑的滑块 http kenwheeler github io slick 和 首先我安装了 jQuery npm 我jquery 然后是光滑的旋转木马 npm 我光滑的轮播 然后我对 angul
  • 插入带有 firefox 扩展名的本地 css 文件

    我正在构建一个 Firefox 扩展 需要在文档中插入一些元素和 css 我尝试以下Firefox 扩展如何将本地 css 文件注入网页 https stackoverflow com questions 2731736 how can a
  • 以编程方式将EnvironmentProperty设置为ChromeDriver

    我正在无头中使用 SE2 和 firefoxDriver 运行测试 环境 Xvfb 使用 FirefoxDriver 我可以非常设置 DISPLAY 环境属性 容易地 FirefoxBinary firefox new FirefoxBin
  • 如何从可视化中删除 D3 链接文本

    当单击力定向可视化中的节点时 任何子节点 及其关联的链接 都会打开 关闭 但是 当删除其关联的子节点和链接时 充当这些链接标签的文本不会被删除 见下文 这是代码的相关部分 最后一行 linkText exit remove 是我删除这些标签
  • Gradle 构建 null 控制台对象

    我正在尝试使用堆栈溢出中的示例让我的 gradle 构建在控制台提示输入密码 当我有这样的陈述时 def password System console readLine Enter keystore password 当我运行时出现错误
  • 如何在 Blackberry Storm 中设置抗锯齿?

    我正在绘制位图 例如 bitmap i new Bitmap 60 60 Graphics g new Graphics bitmap i g setColor Color BLACK g drawLine 现在如何在 g drawLine
  • 使用无符号索引执行反向“for”循环的最佳方法是什么?

    我的第一次尝试反向for循环做某事 n 次是这样的 for unsigned int i n 1 i gt 0 i This fails因为在无符号算术 i保证始终大于或等于零 因此循环条件始终为真 幸运的是 在我不得不想知道为什么循环无限
  • 如何在php中将数组存储到会话变量中

    从问题表中返回10个问题 result mysqli query con SELECT question FROM questions ORDER BY rand LIMIT 10 while row mysqli fetch row re
  • Python - 描述符“split”需要“str”对象,但收到“unicode”

    呃 我有现成的代码 并且我确信它确实有效 但我收到以下错误 类型错误 描述符 split 需要 str 对象 但收到了 统一码 这就是整个定义 def assemblePacket self type ipSplit str split s
  • 将图像加载到文件流

    我正在使用加载图像 OpenFileDialog open new OpenFileDialog 选择文件后 打开 会填充多个项目 包括路径 现在我想将文件加载到文件流 或类似的东西 中以通过网络服务发送 这可能吗 thanks 您可以使用
  • 如何在不使用继承的情况下向控制器添加常见操作?

    我需要在不使用继承的情况下向多个控制器添加常见操作 我们所有的控制器都扩展了抽象控制器 而我想要包含的功能在抽象控制器中没有意义 我最初的想法是使用 Mixin 但看起来动作 因为它们是闭包 并没有 混合 到包含 mixin 的控制器中 只
  • C++ 如何断言向量中的所有 std::shared_ptr 都引用某些东西

    当我有一个函数接收一个应该引用某些东西的 智能 指针时 我总是按如下方式开始 class Foo void doSomething const std shared ptr
  • Python 如何获取某一特定点的导数值?

    from sympy import x Symbol x y x 2 dx diff y x 这段代码可以得到y的导数 这很容易dx 2 x 现在我想得到的值dx for x 2 清楚地 dx 2 2 4 when x 2 但是我如何用Py
  • 使用 gdi+ 将 png 转换为 gif (C#)

    我有一个 png 文件 必须将其转换为 gif 文件 里面有一个透明的部分 当我保存它时 透明的部分是黑色的而不是透明的 这是我的代码 FileStream imgStream new FileStream outputFile FileM
  • 关于Python中的关闭文件

    我知道如果在 Python 中不再使用的话 使用 close 来关闭文件是一个好习惯 我尝试打开大量打开的文件 并且不关闭它们 在同一个Python进程中 但没有看到任何异常或错误 Mac 和 Linux 我都试过了 那么 只是想知道Pyt
  • 如何根据当前用户的角色显示或隐藏 aspx 页面的内容

    我有一个 ASP NET 站点 其中每个页面都有一些内容 每个登录用户都有一些特定的角色 我想根据登录用户的角色隐藏和显示页面内容 我怎样才能做到这一点 P S 我了解 ASP NET 中整个表单的身份验证和授权 我的问题是关于页面的内容
  • 如何在Rcpp代码中返回R的NULL?

    假设我有一个 C 代码要使用 Rcpp 编译 并将在 R 中调用 Rcpp export SEXP to env List x if x hasAttribute names return x else return NULL 应该做什么N
  • 在 Android 中集成 Google Plus 时,SERVICE_VERSION_UPDATE_REQUIRED 和 Google Play 服务已过时

    我已经在 Android 应用程序中集成了 Google Plus 我使用的是 Android SDK 版本 4 2 2 但我在 google plus 上执行共享帖子时遇到此错误 Google Plus 服务已过时 and Connect
  • 检查 Python 列表中的项目是否为 int/number

    我有一个 Python 脚本 它读取 csv 文件并将每个值存储到列表列表中 list x y 我对此没有任何问题 list i 0 for row in reader list append list i append row 0 i 1
  • 为什么 std::search 需要前向迭代

    我的问题与下面的线程相同 我正在努力理解给出的答案 或者更确切地说 我的代码不应该工作 因为它只使用输入迭代器 但我的 func 似乎工作并且行为与 std search 相同 所以我不知所措 不愿意在没有正确理解的情况下继续前进 也许如果