C++ search(STL search)算法详解
发布时间:2020-12-16 07:39:48 所属栏目:百科 来源:网络整理
导读:在查找序列的子序列方面,search() 算法和 find_end() 算法相似,但它所查找的是第一个匹配项而不是最后一个。 和 find_end() 算法一样,它也有两个版本,第二个版本接受用来比较元素的谓词作为第 5 个参数。可以用 search() 来验证前面使用 find_end() 搜索
在查找序列的子序列方面,search() 算法和 find_end() 算法相似,但它所查找的是第一个匹配项而不是最后一个。 和 find_end() 算法一样,它也有两个版本,第二个版本接受用来比较元素的谓词作为第 5 个参数。可以用 search() 来验证前面使用 find_end() 搜索的结果。 如何改变每次遍历搜索的具体范围是它们的主要不同之处。下面是一个示例: string text {"Smith,where Jones had had "had",had had "had had"."" "Had had" had had the examiners' approval."}; std::cout << text << std::endl; string phrase {"had had"}; size_t count {}; auto iter = std::begin(text); auto end_iter = end(text); while((iter = std::search(iter,end_iter,std::begin(phrase),std::end (phrase),[](char ch1,char ch2) { return std::toupper (ch1) == std:: toupper (ch2); })) != end_iter) { ++count; std::advance(iter,phrase.size()); // Move to beyond end of subsequence found } std::cout << "n""<< phrase << "" was found "<< count << " times." << std::endl;这段代码执行后会输出下面的内容:
Smith,where Jones had had "had",had had "had had". "Had had" had had the examiners' approval. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- ruby-on-rails – 仅验证数字
- ruby-on-rails – 如果空闲,Rails protect_from_
- c# – 这是使用多态的正确方法吗?
- applicationContext.xml文件中class中的内容不能
- mssql 支持的分页+sqlite + mysql 的分页语句
- c# – 访问Web用户控件上的母版页控件
- 依赖、依赖倒转原则、IoC/依赖注入、以及在Sprin
- 依赖注入 – 将JS JS SDK注入AngularJS控制器
- linux 远程连接ssh提示IT IS POSSIBLE THAT SOME
- c# – 从ASP.NET Web Handler(.ashx)下载文件时的
热点阅读