search原型:
std::search
equality (1)
template <class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2);
|
predicate (2)
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
BinaryPredicate pred);
|
该函数是查找[first2,last2)第一次出现在[first1,last1)中的位置。
也就相当于一个子序列在一个序列中第一次出现的位置。
若匹配成功,返回[first1,last1)中匹配的第一个相应元素。
否则,返回last1.
行为类似于:
template<class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 search ( ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2)
{
if (first2==last2) return first1; // specified in C++11
while (first1!=last1)
{
ForwardIterator1 it1 = first1;
ForwardIterator2 it2 = first2;
while (*it1==*it2) { // or: while (pred(*it1,*it2)) for version 2
++it1; ++it2;
if (it2==last2) return first1;
if (it1==last1) return last1;
}
++first1;
}
return last1;
}
一个简单的例子:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void msearch(){
char vc[]={"iamastudentstuden"};
char vt[]={"student"};
auto it=search(vc,vc+17,vt,vt+7);
cout<<"auto it=search(vc,vc+17,vt,vt+7);;"<<endl;
for(auto i=it;i!=vc+17;++i)
cout<<*i;
cout<<endl;
}
运行截图:
search_n原型:
std::search_n
equality (1)
template <class ForwardIterator, class Size, class T>
ForwardIterator search_n (ForwardIterator first, ForwardIterator last,
Size count, const T& val);
|
predicate (2)
template <class ForwardIterator, class Size, class T, class BinaryPredicate>
ForwardIterator search_n ( ForwardIterator first, ForwardIterator last,
Size count, const T& val, BinaryPredicate pred );
|
该函数是查找序列中连续count个val值第一次出现的位置。
如果有,则返回第一次出现的首元素,否则返回last。
其行为类似于:
template<class ForwardIterator, class Size, class T>
ForwardIterator search_n (ForwardIterator first, ForwardIterator last,
Size count, const T& val)
{
ForwardIterator it, limit;
Size i;
limit=first; std::advance(limit,std::distance(first,last)-count);
while (first!=limit)
{
it = first; i=0;
while (*it==val) // or: while (pred(*it,val)) for the pred version
{ ++it; if (++i==count) return first; }
++first;
}
return last;
}
一个简单的例子:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool mypredicate (int i, int j) {
return (i==j);
}
void msearchn(){
int myints[]={10,20,30,30,20,10,10,20};
std::vector<int> myvector (myints,myints+8);
std::vector<int>::iterator it;
// using default comparison:
it = std::search_n (myvector.begin(), myvector.end(), 2, 30);
if (it!=myvector.end())
std::cout << "two 30s found at position " << (it-myvector.begin()) << '\n';
else
std::cout << "match not found\n";
// using predicate comparison:
it = std::search_n (myvector.begin(), myvector.end(), 2, 10, mypredicate);
if (it!=myvector.end())
std::cout << "two 10s found at position " << int(it-myvector.begin()) << '\n';
else
std::cout << "match not found\n";
}
运行截图:
——————————————————————————————————————————————————————————————————
//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
author:天下无双
Email:coderguang@gmail.com
2014-9-26
于GDUT
——————————————————————————————————————————————————
分享到:
相关推荐
algorithm algorithm STL 算法 algorithm_头文件_说明 algorithm algorithm STL 算法 algorithm_头文件_说明 algorithm algorithm STL 算法 algorithm_头文件_说明
用于三维光学形貌扫描完成后,生成的stl文件的读取,并形成俯视投影云图
C++读取STL文件,输出所有三角形的顶点坐标
09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板...
STL学习教程,
最新的STL源码,最新的STL源码,最新的STL源码
stlshow_stl分层_STL分层_stlmatlab_STL切片_stl分层.zip
C++ STL 参考手册Cpp_STL_ReferenceManual ,对学习STL 有帮助
STL 最新源代码
STL切片算法,一种快速高效解决stl文件切片的程序算法讲解
stl的入门ppt 很好用哦,分为导入 STL的概念与组成 Iterator(迭代器) Container(容器) Algorithm(算法) Adaptors(配接器)
stlshow_stl分层_STL分层_stlmatlab_STL切片_stl分层_源码.rar.rar
sgi-STL的相关资料.有代码和帮助文件
看STL文件的小软件,可以自由的实现旋转,等功能,现在只是一小部分,以后会发后面的
西门子S7-300语句表 STL 实例源代码
读取stl文件,并将读取的数据显示在屏幕上
stl转换,能够有效的将matlab 转化为stl文件,用于3DMAX的绘图
基于VC++和OpenGL的STL文件读取显示STL是三维模型常用的文件格式。对STL文件进行读取和显示,是对模型进行后续操作的前提。在对STL文件格式进行详细分析的基础上,以VC+-I-作为开发平台
十多个STL的经典例子,相当不错,初学STL必备
在matlab中读取点云数据stl文件并在界面显示