我的问题与下面的线程相同,我很难理解给出的答案,或者更确切地说,我的代码不应该工作,因为它只使用输入迭代器..但我的 func 似乎工作并且行为与 std 相同: :search ..所以我很茫然,不愿意在没有正确理解的情况下继续前进......也许如果有人可以提出一个会破坏我的功能但不会破坏 std::
的输入来自 Why do I need a Forward Iterator to implement my customized std::search :
I am studying the book "Accelerated C++" from Koenig & Moo.
Exercise 8-2 ask me to implement on my own some templatized functions from <algorithm> and <numeric>, and to specify what kind of iterator does my implementation require.
When trying to implement std::search, I determined that I need only "input" iterators.
However, looking at the implementation of std::search installed with my compiler, I can see that they use "forward" iterators, but I cannot understand why, because there is no need to write, only to read, and input iterators meet the requirement.
Can anybody here help me to understand this, please? Why would I need to use "forward" iterators to implement std::search?
Thanks in advance.
我的函数:
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,您需要能够多次遍历序列的各个部分。
关于c++ - 为什么 std::search 需要转发迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5368460/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
我在开发的Rails3网站的一些搜索功能上遇到了一个小问题。我有一个简单的Post模型,如下所示:classPost我正在使用acts_as_taggable_on来更轻松地向我的帖子添加标签。当我有一个标记为“rails”的帖子并执行以下操作时,一切正常:@posts=Post.tagged_with("rails")问题是,我还想搜索帖子的标题。当我有一篇标题为“Helloworld”并标记为“rails”的帖子时,我希望能够通过搜索“hello”或“rails”来找到这篇帖子。因此,我希望标题列的LIKE语句与acts_as_taggable_on提供的tagged_with方法
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我注意到像bundler这样的项目在每个specfile中执行requirespec_helper我还注意到rspec使用选项--require,它允许您在引导rspec时要求一个文件。您还可以将其添加到.rspec文件中,因此只要您运行不带参数的rspec就会添加它。使用上述方法有什么缺点可以解释为什么像bundler这样的项目选择在每个规范文件中都需要spec_helper吗? 最佳答案 我不在Bundler上工作,所以我不能直接谈论他们的做法。并非所有项目都checkin.rspec文件。原因是这个文件,通常按照当前的惯例,只
它不等于主线程的binding,这个toplevel作用域是什么?此作用域与主线程中的binding有何不同?>ruby-e'putsTOPLEVEL_BINDING===binding'false 最佳答案 事实是,TOPLEVEL_BINDING始终引用Binding的预定义全局实例,而Kernel#binding创建的新实例>Binding每次封装当前执行上下文。在顶层,它们都包含相同的绑定(bind),但它们不是同一个对象,您无法使用==或===测试它们的绑定(bind)相等性。putsTOPLEVEL_BINDINGput