find和find_if,value_type
find算法:返回 [first,end)中第一个值等于value元素的位置
线性复杂度:最多比较次数:元素的总个数
find函数的最后一个参数,必须是string,float,char,double,int等,用自定义类型的数据查找会出错。
要想确切知道在容器中的位置,要用distance(容器.begin(),p)+1,//p是迭代器返回的位置,+1看情况,看下标是从几开始的。数组的下标是从1开始的,
为了更好理解,我们举个例子:
1、数组在内存中申请是,所申请的内存是一段连续的内存地址;
2、例:int[] a=new int[3];申请一段:int 数据类型的数组,a 为变量,数组长度为:[3];
3、这个数组所申请的内存地址是连续的(假设所申请的:第一个内存地址为:1008,第二个为:1009,第三个为:1010);,但我们只知道:一、变量:a,它只拿到第一个内存地址1008;二、它的数组空间为3个;
4、a[0]——把a拿到的内存地址:1008 + 0 = 1008 (指向第一个内存地址);
a[1]——把a拿到的内存地址:1008 + 1 = 1009 (指向第二个内存地址);
a[2]——把a拿到的内存地址:1008 + 2 = 1010 (指向第三个内存地址);
所以:数据下标从 [0] 开始的意义也在于此!(当然,这是理解版的)。
#include "stdafx.h"
#include<iostream>
#include<functional>
#include<algorithm>
#include<vector>
#include<numeric>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int array[] = { 30, 50, 70, 90, 20, 10 };
int n = sizeof(array) / sizeof(int);
vector<int>b(array, array + n);
vector<int>::iterator p = find(b.begin(), b.end(), 50);
cout << "it has exit,and the possion is " << distance(b.begin(), p) + 1 << endl;
if (p != b.end())
{
cout << "it has exit,the place is :" << distance(b.begin(), p)+1 << endl;
}
else
cout << "it not exit" << endl;
return 0;
}
find_if:在区间 [first,end)中搜寻使一元判断式pred为true的第一个元素,
重载(),是个仿函数,在运算符()内部定义要查找的条件,仿函数返回类型必须是bool类型,客观反映在find_if函数查找过程中是否有匹配。
value_type:是stl容器中的数据的数据类型,即迭代器所指对象的类别,在使用stl模板时,需要传入迭代器的参数,这个参数的类别就是容器中数据的类别, 那个参数类型的别名就叫value_type。
自定义类:
#include "stdafx.h"
#include<iostream>
#include<functional>
#include<algorithm>
#include<vector>
#include<numeric>
using namespace std;
template<class T>
class intgreat
{
public:
bool operator()(T &num)
{
return num > 50;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
int array[] = { 30, 50, 70, 90, 20, 10 };
int n = sizeof(array) / sizeof(int);
vector<int>b(array, array + n);
vector<int>::iterator p = find_if(b.begin(), b.end(),intgreat<int>() );
cout << "it has exit,and the possion is " << distance(b.begin(), p) + 1 << endl;
cout <<*p << endl;
if (p != b.end())
{
cout << "it has exit,the place is :" << distance(b.begin(), p)+1 << endl;
}
else
cout << "it not exit" << endl;
return 0;
自定义结构体的查找函数
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
struct value_t
{
int a;
int b;
};
class vector_finder
{
public:
vector_finder( const int a, const int b ) :m_v_a(a),m_v_b(b){}
bool operator ()( vector<struct value_t>::value_type &value) //vector<struct value_t>::value_type这部分都是类型说明,,迭代器所指对象的类型,即容器中数据的数据类型
{
return (value.a==m_v_a)&&(value.b = m_v_b);
}
private:
int m_v_a;
int m_v_b;
};
int main()
{
vector<value_t> my_vector;
value_t my_value;
my_value.a = 11; my_value.b = 1001;
my_vector.push_back(my_value);
my_value.a = 12; my_value.b = 1002;
my_vector.push_back(my_value);
my_value.a = 13; my_value.b = 1003;
my_vector.push_back(my_value);
my_value.a = 14; my_value.b = 1004;
my_vector.push_back(my_value);
vector<value_t>::iterator it = find_if( my_vector.begin(), my_vector.end(), vector_finder(13,1003)); //传入数值,首先执行构造函数初始化,然后将my_vector中的数值依次传入vector_finder中查找
if( it == my_vector.end() )
cout<<"not found!"<<endl;
else
cout<<"found value a:"<<(*it).a <<", b:"<<(*it).b<<endl;
return 0;
}
在map中的应用:
#include <iostream>
#include <map>
#include <string>
#include <algorithm>
using namespace std;
class map_finder
{
public:
map_finder( string cmp_string ) : m_string(cmp_string) {}
bool operator () (const map<int,string>::value_type pair)
{
return pair.second == m_string;
}
private:
string m_string;
};
int main()
{
map<int ,string> my_map;
my_map.insert( make_pair(10,"china"));
my_map.insert( make_pair(20,"usa"));
my_map.insert( make_pair(30,"english"));
my_map.insert( make_pair(40,"hongkong"));
map<int,string>::iterator it = find_if(my_map.begin(),my_map.end(),map_finder("english"));
if( it == my_map.end() )
cout<<"not found!"<<endl;
else
cout<<"found key:"<<(*it).first<<", value:"<<(*it).second<<endl;
return 0;
}
查找map中的数值,也可以通过数据来查找,不一定非要关键字。
find和find_if,value_type的更多相关文章
- STL的std::find和std::find_if
std::find是用来查找容器元素算法,但是它只能查找容器元素为基本数据类型,如果想要查找类类型,应该使用find_if. 小例子: #include "stdafx.h" #i ...
- STL中的find_if函数
上一篇文章也讲过,find()函数只能处理简单类型的内容,也就是缺省类型,如果你想用一个自定义类型的数据作为查找依据则会出错!这里将讲述另外一个函数find_if()的用法 这是find()的一个 ...
- map按value值查找——find_if的使用(转载)
转载:http://www.cnblogs.com/xufeiyang/archive/2012/05/09/2491871.html CValueFind #ifndef _CVALUEFIND_H ...
- 实战c++中的vector系列--vector应用之STL的find、find_if、find_end、find_first_of、find_if_not(C++11)
使用vector容器,即避免不了进行查找,所以今天就罗列一些stl的find算法应用于vector中. find() Returns an iterator to the first element ...
- C++ STL算法系列2---find ,find_first_of , find_if , adjacent_find的使用
一.find运算 假设有一个int型的vector对象,名为vec,我们想知道其中是否包含某个特定值. 解决这个问题最简单的方法时使用标准库提供的find运算: 1 // value we'll lo ...
- stl::find,find_if,find_if_not
//满足特定条件下的实现,回调函数template<class InputIt, class UnaryPredicate> InputIt find_if(InputIt first, ...
- find_if函数与partition函数的转换
编写程序,求大于等于一个给定长度的单词有多少.我们还会修改输出,使程序只打印大于等于给定长度的单词. 使用find_if实现的代码如下: #include<algorithm> #incl ...
- STL 查找vector容器中的指定对象:find()与find_if()算法
1 从vector容器中查找指定对象:find()算法 STL的通用算法find()和find_if()可以查找指定对象,参数1,即首iterator指着开始的位置,参数2,即次iterator指着停 ...
- c++ stl algorithm: std::find, std::find_if
std::find: 查找容器元素, find仅仅能查找容器元素为<基本数据类型> [cpp] view plaincopy #include <iostream> #incl ...
随机推荐
- spring使用中ModelAttribute的内容被覆盖
在前台以get方式向后台提交数据: 后台接收: 后台接收参数的时候,由于user里面也有一个属性为id,后台在接收参数的时候,User里面的id会被重新赋值,这是一个大坑.如果后续继续用User来做操 ...
- Hibernate学习笔记3.1(Hibernate关系映射)
主要指对象之间的关系 1.一对一关联 一对一单项外键关联 比如说一夫一妻 Wifi.java package com.bjsxt.hibernate; import javax.persistence ...
- nagiosQL访问时报错PHP message: PHP Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead
nagiosQL安装环境: CentOS release 6.4 (Final) php-5.5.4 nagiosql_320 nginx version: nginx/1.2.3 安装一切正常,当访 ...
- 将一个dropdownlist从一个div复制到另一个div
<select id="dropdwon1"> <option value=">Item1</option> <option v ...
- Pandas数据排序
Pandas数据排序 .sort_index() 在指定轴上根据索引进行排序,索引排序后内容会跟随排序 b = pd.DataFrame(np.arange(20).reshape(4,5),inde ...
- java中garadle工程没有src问题
https://www.jb51.net/article/142791.htm 前几天遇到一个问题,就是使用ider创建gradle项目后,src目录没有自动生成出来,今天就给大家分享一下怎么解决. ...
- Java中String的intern方法,javap&cfr.jar反编译,javap反编译后二进制指令代码详解,Java8常量池的位置
一个例子 public class TestString{ public static void main(String[] args){ String a = "a"; Stri ...
- /src/log4j.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration S ...
- ubuntu下mysql源码编译安装
建议:cpu4核以上,内存4G以上 1. 安装环境:Ubuntu Server 14.10MySQL-5.6.23.tar.gz 2. 安装必备的工具sudo apt-get install make ...
- zxing解析带logo二维码会报com.google.zxing.NotFoundException
参考原文:https://blog.csdn.net/cyl1226541/article/details/70557232 //复杂模式,开启PURE_BARCODE模式(☆☆☆) HINTS.pu ...