<algorithm>find函数使用
2016-11-17 20:14
148 查看
近日,阅读STL源码剖析,其中的find函数与我平时使用的感觉有点差别。于是到c++ reference中去深入了解了一下。
c++ reference里边是这样写到的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;
}
其中的返回值是这样描述的:An iterator to the first element in the range that compares equal to
val.If no elements match, the function returns last.大致意思就是返回第一个与所查询值相同的迭代器,如果没找到则返回end迭代器。
下面是一个例子:#include <iostream>
#include <iterator>
#include <vector>
int main()
{
std::vector<int> my_Vector;
std::istream_iterator<int> startInput(std::cin), endInput; /*定义输入流迭代器*/
//使用输入流迭代器向容器中添加数据
while (startInput != endInput)
my_Vector.push_back(*startInput++);
/*查找容器中的元素*/
std::vector<int>::iterator iter;
iter = std::find(my_Vector.begin(), my_Vector.end(), 12);
if (iter != my_Vector.end())
std::cout << "The number is found:" << *iter << std::endl;
else
std::cout << "The number is not found" << std::endl;
return 0;
}这个例子应该可以帮助一部分人了解一下find函数
c++ reference里边是这样写到的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;
}
其中的返回值是这样描述的:An iterator to the first element in the range that compares equal to
val.If no elements match, the function returns last.大致意思就是返回第一个与所查询值相同的迭代器,如果没找到则返回end迭代器。
下面是一个例子:#include <iostream>
#include <iterator>
#include <vector>
int main()
{
std::vector<int> my_Vector;
std::istream_iterator<int> startInput(std::cin), endInput; /*定义输入流迭代器*/
//使用输入流迭代器向容器中添加数据
while (startInput != endInput)
my_Vector.push_back(*startInput++);
/*查找容器中的元素*/
std::vector<int>::iterator iter;
iter = std::find(my_Vector.begin(), my_Vector.end(), 12);
if (iter != my_Vector.end())
std::cout << "The number is found:" << *iter << std::endl;
else
std::cout << "The number is not found" << std::endl;
return 0;
}这个例子应该可以帮助一部分人了解一下find函数
相关文章推荐
- <algorithm> 中sort的使用
- STL<algorithm>中的nth_element()方法的使用
- 解决“单击以激活并使用此控件”的办法(解决flash)<转>
- jstl之<c:forEach>的使用
- WF4B1 的Procedural Activity 之InvokeMethod , InvokeMethod<T> 使用
- 【泛型】使用List&lt;T&gt;篇
- JSP页面<% ... %>中怎样使用out及其它
- 在IE浏览器中使用Windows窗体控件<转自网络>
- WF4B1 的Procedural Activity 之Assign,WriteLine,IF,While,DoWhile,Switch<T>使用
- 【转】如何使用 <fstream> 类进行文件的 I/O 处理
- ArcGIS API for Silverlight开发入门(7):使用非AGS数据源的图层<转>
- 《Windows程序设计》读书笔记------------->>滚动条使用(翻译自MSDN)<<
- setInterval和setTimeout的使用区别(转自 <a href="http://www.cnblogs.com/sk-net/archive/2008/01/24/1050770.html">http://www.cnblogs.com/sk-net/archive/2008/01/24/1050770.html</a>)
- 想实现自己的地理应用程序?建议参考<<使用Rails和Ajax开发Google地图应用程序>>
- WF4B1 的Procedural Activity 之Sequence,Delay,Pick,PickBranch,Parallel,ForEach,ForEach<T>使用
- C#基础学习日志===&amp;amp;amp;amp;gt;命名空间的使用&amp;amp;amp;amp;lt;===
- 使用lucene.net2.0 搜索数据库的例子<转自csdn>
- ArcGIS API for Silverlight开发入门(8):在程序中使用Virtual Earth的服务<转>
- arcgis api for flex 开发入门(四)Darw控件的使用<转>
- Struts中用<bean:size>和<bean:write>的使用,用于显示集合的总记录数