原文地址:http://www.cplusplus.com/reference/algorithm/find/
function template
<algorithm>
std::find
template <class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& val);
Find value in range
Returns an iterator to the first element in the range[first,last)
that compares equal toval. If no such element is found, the function returnslast.
返回范围内与val相等的第一个元素的迭代器,如果没有这样的元素,那么将返回last.
例子:
#include <iostream>
#include <algorithm>
#include <vector>
#include <array>
using namespace std;
void find2(){
vector<int> vi{1,5,7,8,9,9,8,5,9};
cout<<"vi=";
for(int &i:vi)
cout<<i<<" ";
cout<<endl;
auto it=find(vi.begin(),vi.end(),8);
cout<<"auto it=find(vi.begin(),vi.end(),8);"<<endl;
cout<<"(it-1)="<<*(it-1)<<endl;
cout<<"it="<<*it<<endl;
cout<<"(it+1)="<<*(it+1)<<endl;
auto it2=find(vi.begin(),vi.end(),18);
cout<<"auto it2=find(vi.begin(),vi.end(),18);"<<endl;
cout<<"(it2-1)="<<*(it2-1)<<endl;
cout<<"it2="<<*it2<<endl;
cout<<"(it2+1)="<<*(it2+1)<<endl;
if(it2==vi.end())
cout<<"it2==vi.end()"<<endl;
}
运行截图:
The function usesoperator==
to compare the individual elements toval.
使用operator==来比较元素与val是否相等。
The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
|
template<class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& val)
{
while (first!=last) {
if (*first==val) return first;
++first;
}
return last;
}
|
|
Parameters
first, last
Input iteratorsto the initial and final positions in a sequence. The range searched
is[first,last)
, which contains all the elements betweenfirstandlast, including the element pointed byfirstbut not the element pointed bylast.
比较的范围。
val
Value to search for in the range.
Tshall be a type supporting comparisons with the elements pointed byInputIteratorusingoperator==
(with the elements as left-hand side operands, andvalas right-hand side).
比较的值。
Return value
An iterator to the first element in the range that compares equal toval.
If no elements match, the function returnslast.
返回范围内与val相等的第一个元素的迭代器,如果没有这样的元素,那么将返回last.
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
// find example
#include <iostream> // std::cout
#include <algorithm> // std::find
#include <vector> // std::vector
int main () {
// using std::find with array and pointer:
int myints[] = { 10, 20, 30, 40 };
int * p;
p = std::find (myints, myints+4, 30);
if (p != myints+4)
std::cout << "Element found in myints: " << *p << '\n';
else
std::cout << "Element not found in myints\n";
// using std::find with vector and iterator:
std::vector<int> myvector (myints,myints+4);
std::vector<int>::iterator it;
it = find (myvector.begin(), myvector.end(), 30);
if (it != myvector.end())
std::cout << "Element found in myvector: " << *it << '\n';
else
std::cout << "Element not found in myints\n";
return 0;
}
|
|
Output:
Element found in myints: 30
Element found in myvector: 30
|
Complexity
Up to linear in thedistancebetweenfirstandlast: Compares elements until a match is found.
Data races
Some (or all) of the objects in the range[first,last)
are accessed (once at most).
Exceptions
Throws if either an element comparison or an operation on an iterator throws.
Note that invalid arguments causeundefined behavior.
——————————————————————————————————————————————————————————————————
//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
author:天下无双
Email:coderguang@gmail.com
2014-9-11
于GDUT
——————————————————————————————————————————————————————————————————
分享到:
相关推荐
STL所有算法介绍STL所有算法介绍STL所有算法介绍
STL常用算法简介 STL常用算法简介 STL常用算法简介 STL常用算法简介 STL常用算法简介
stl常用算法,模板stl常用算法,
STL 常用算法大全 STL(Standard Template Library)提供了丰富的算法库,涵盖了查找、排序、数值计算等多方面的算法。这些算法可以帮助开发者快速、高效地处理数据。 查找算法(13 个) 1. `adjacent_find`:在...
参考自侯捷先生的《STL源码剖析》,C++ STL 的数值算法(Numeric algorithms)是一组对容器元素进行数值计算的模板函数,包括容器元素求和 accumulate 、两序列元素的内积 inner_product 、容器元素的一系列部分元素和...
STL和常用算法简介
ACM STL算法入门 导入 STL的概念与组成 Iterator(迭代器) Container(容器) Algorithm(算法) Adaptors(配接器)
algorithm algorithm STL 算法 algorithm_头文件_说明 algorithm algorithm STL 算法 algorithm_头文件_说明 algorithm algorithm STL 算法 algorithm_头文件_说明
自己根据编程原本与使用C++的经历写的STL的algorithm的实现,不好的地方请指出
STL常用算法 七十个 12
C++ stl算法汇总 STL 各种算法 应用 大全
C++精选代码库。包含常用STL容器模拟实现、algorithm算法头文件函数demo
STL切片算法,一种快速高效解决stl文件切片的程序算法讲解
C++库函数及STL算法(英文版)介绍及用法 每个函数都有简单的源文件说明,有例子
C++STL中algorithm里find()函数 1.string中的运用 1.1函数原型及描述 函数1 size_type find(const string & str, size_type pos = 0) const 形参列表str:要查找的子字符串string变量,pos :要查找的起始位位置 ...
STL算法部分主要由头文件<algorithm>,,组成。要使用 STL中的算法函数必须包含头文件<algorithm>,对于数值算法须包含,中则定义了一些模板类,用来声明函数对象。
该文档收录了STL中的部分常用的算法,非常实用。。。对于ACM编程着很大的帮助。。。
stl学习资料说明,stl基本使用说明,stl常用算法说明
关于stl的一些用法 STL算法作为模板函数提供 STL另一个重要特性是它不是面向对象的。为了具有足够通用性,STL主要依赖于模板而不是封装,继承和虚函数(多态性)——OOP的三个要素。
STL算法一览...............