简单的程序诠释C++ STL算法系列之二:find
2011-09-29 00:24
337 查看
C++STL的非变易算法(Non-mutating algorithms)是一组不破坏操作数据的模板函数,用来对序列数据进行逐个处理、元素查找、子序列搜索、统计和匹配。
find算法用于查找等于某值的元素。它在迭代器区间[first , last)上查找等于value值的元素,如果迭代器iter所指的元素满足 *iter == value ,则返回迭代器iter,未找则返回last。
函数原型:
示例代码:
*******************************************************************************************************************************
C++经典书目索引及资源下载:/article/1443593.html
********************************************************************************************************************************
find算法用于查找等于某值的元素。它在迭代器区间[first , last)上查找等于value值的元素,如果迭代器iter所指的元素满足 *iter == value ,则返回迭代器iter,未找则返回last。
函数原型:
template<class InputIterator, class Type> InputIterator find( InputIterator _First, InputIterator _Last, const Type& _Val );
示例代码:
/******************************************************************* * Copyright (C) Jerry Jiang * File Name : find.cpp * Author : Jerry Jiang * Create Time : 2011-9-29 0:03:25 * Mail : jbiaojerry@gmail.com * Blog : http://blog.csdn.net/jerryjbiao * Description : 简单的程序诠释C++ STL算法系列之二 * 非变易算法 : 查找容器元素find ******************************************************************/ #include <algorithm> #include <list> #include <iostream> using namespace std; int main() { list<int> ilist; for (size_t i = 0; i < 10; ++i) { ilist.push_back(i+1); } ilist.push_back(10); list<int>::iterator iLocation = find(ilist.begin(), ilist.end(), 10); if (iLocation != ilist.end()) { cout << "找到元素 10" << endl; } cout << "前一个元素为:" << *(--iLocation) << endl; return 0; }
*******************************************************************************************************************************
C++经典书目索引及资源下载:/article/1443593.html
********************************************************************************************************************************
相关文章推荐
- 简单的程序诠释C++ STL算法系列之二:find
- 简单的程序诠释C++ STL算法系列之二:find
- 简单的程序诠释C++ STL算法系列之十二:find_end
- 简单的程序诠释C++ STL算法系列之四:adjacent_find
- 简单的程序诠释C++ STL算法系列之五:find_first_of
- 简单的程序诠释C++ STL算法系列之三:find_if
- 简单的程序诠释C++ STL算法系列之三:find_if
- 简单的程序诠释C++ STL算法系列之四:adjacent_find
- 简单的程序诠释C++ STL算法系列: find & find_if
- 简单的程序诠释C++ STL算法系列之十六:iter_swap
- 简单的程序诠释C++ STL算法系列之一:for_each
- 简单的程序诠释C++ STL算法系列之十:search
- 【转】简单的程序诠释C++ STL算法系列之十五:swap
- 置顶] 简单的程序诠释C++ STL算法系列之十三:copy http://blog.csdn.net/jerryjbiao/article/details/7376088
- 简单的程序诠释C++ STL算法系列之十六:iter_swap
- 简单的程序诠释C++ STL算法系列之十八:transform
- 简单的程序诠释C++ STL算法系列之八:mismatch
- 简单的程序诠释C++ STL算法系列之九:equal
- 简单的程序诠释C++ STL算法系列之十七:swap_ranges
- 简单的程序诠释C++ STL算法系列之一:for_each