adjacent_find()

在iterator对标识元素范围内,查找一对相邻重复元素,找到则返回指向这对元素的第一个元素的迭代器。否则返回past-the-end。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

void play_adjacent_find()
{
	vector<int> v1;
	v1.push_back(1);
	v1.push_back(2);
	v1.push_back(2);
	v1.push_back(3);
	v1.push_back(5);

	vector<int>::iterator it = adjacent_find(v1.begin(), v1.end());
	if (it == v1.end())
	{
		cout << "没有找到 重复的元素" << endl;
	}
	else
	{
		cout << *it << endl;
	}
	// 2
	int index = distance(v1.begin(), it);
	cout << index << endl;
	// 1

}

int main()
{
	play_adjacent_find();

	return 0;
}

binary_search()

在有序序列中查找value,找到则返回true。注意:在无序序列中,不可使用。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

// binary_search是对排序好的进行查找
void play_binary_search()
{
	vector<int> v1;
	v1.push_back(1);
	v1.push_back(3);
	v1.push_back(5);
	v1.push_back(7);
	v1.push_back(9);

	bool b = binary_search(v1.begin(), v1.end(), 7);
	if (b) {
		cout << "find success\n";
	}
	else {
		cout << "find fail\n";
	}
	// find success

}

int main()
{
	play_binary_search();

	return 0;
}

count()

利用等于操作符,把标志范围内的元素与输入值比较,返回相等的个数。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

void play_count()
{
	vector<int> v1;
	v1.push_back(1);
	v1.push_back(3);
	v1.push_back(3);
	v1.push_back(7);
	v1.push_back(9);
	v1.push_back(3);

	int cnt = count(v1.begin(), v1.end(), 3);
	cout << "count of 3: " << cnt << endl;
	// count of 3: 3

}

int main()
{
	play_count();

	return 0;
}

count_if()

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

bool GreaterThree(const int &a)
{
	return a > 3;
}

void play_count_if()
{
	vector<int> v1;
	v1.push_back(1);
	v1.push_back(3);
	v1.push_back(4);
	v1.push_back(7);
	v1.push_back(9);
	v1.push_back(3);

	int cnt = count_if(v1.begin(), v1.end(), GreaterThree);
	cout << "count of greater 3: " << cnt << endl;
	// count of greater 3: 3

}

int main()
{
	play_count_if();

	return 0;
}

find()

find:  利用底层元素的等于操作符,对指定范围内的元素与输入值进行比较。当匹配时,结束搜索,返回该元素的迭代器。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

void play_find()
{
	vector<int> v1;
	v1.push_back(1);
	v1.push_back(3);
	v1.push_back(4);
	v1.push_back(7);
	v1.push_back(9);
	v1.push_back(3);

	vector<int>::iterator it = find(v1.begin(), v1.end(), 4);
	if (it == v1.end()) {
		cout << "find fail\n";
	}
	else {
		cout << "find success\n";
	}
	// find success

}

int main()
{
	play_find();

	return 0;
}

find_if()

find_if: 使用输入的函数代替等于操作符执行find。返回被找到的元素的迭代器。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

bool GreaterThree(const int &a)
{
	return a > 3;
}

void play_find_if()
{
	vector<int> v1;
	v1.push_back(1);
	v1.push_back(3);
	v1.push_back(4);
	v1.push_back(7);
	v1.push_back(9);
	v1.push_back(3);

	vector<int>::iterator it = find_if(v1.begin(), v1.end(), GreaterThree);
	if (it == v1.end()) {
		cout << "find fail\n";
	}
	else {
		cout << "find success\n";
		cout << "value: " << *it << endl;
	}
	// find success
	// value: 4

}

int main()
{
	play_find_if();

	return 0;
}

STL常用查找算法介绍的更多相关文章

  1. C++ STL 常用查找算法

    C++ STL 常用查找算法 adjacent_find() 在iterator对标识元素范围内,查找一对相邻重复元素,找到则返回指向这对元素的第一个元素的迭代器.否则返回past-the-end. ...

  2. STL常用排序算法介绍

    merge()  以下是排序和通用算法:提供元素排序策略  merge: 合并两个有序序列,存放到另一个序列. #include <iostream> #include <cstdi ...

  3. 常用查找算法(Java)

    常用查找算法(Java) 2018-01-22 1 顺序查找 就是一个一个依次查找 2 二分查找 二分查找(Binary Search)也叫作折半查找. 二分查找有两个要求, 一个是数列有序, 另一个 ...

  4. C++ STL 常用排序算法

    C++ STL 常用排序算法 merge() 以下是排序和通用算法:提供元素排序策略 merge: 合并两个有序序列,存放到另一个序列. 例如: vecIntA,vecIntB,vecIntC是用ve ...

  5. C++ STL 常用遍历算法

    C++ STL 常用遍历算法 STL的容器算法迭代器的设计理念 1) STL的容器通过类模板技术,实现数据类型和容器模型的分离 2) STL的迭代器技术实现了遍历容器的统一方法:也为STL的算法提供了 ...

  6. C++ STL之查找算法

    C++STL有好几种查找算法,但是他们的用法上有很多共同的地方: 1.除了binary_search的返回值是bool之外(查找的了返回true,否则返回false),其他所有的查找算法返回值都是一个 ...

  7. STL常用遍历算法for_each和transform的比较

    for_each()和transform()算法比较 1)STL 算法 – 修改性算法  for_each()  copy()  copy_backward()  transform()  merge ...

  8. C语言实现常用查找算法——二分查找

    #include<stdio.h> void insert_sort(int a[],int n); int binary_search(int a[],int x,int n); voi ...

  9. python实现常用查找算法

    http://www.cnblogs.com/feixuelove1009/p/6148357.html

随机推荐

  1. 浅谈机器人控制与仿真设计----RDS和ROS

    机器人控制.仿真或实验,主要由三个部分组成,机器人.环境和算法. 当然各部分又包含很多子部分和功能,这里主要以仿真为主,为了使得仿真结果能够直接应用到实际机器人上,这里分别以RDS和ROS对比介绍.h ...

  2. 给定一个实数数组,按序排列(从小到大),从数组从找出若干个数,使得这若干个数的和与M最为接近,描述一个算法,并给出算法的复杂度。

    有N个正实数(注意是实数,大小升序排列) x1 , x2 ... xN,另有一个实数M. 需要选出若干个x,使这几个x的和与 M 最接近. 请描述实现算法,并指出算法复杂度. #define M 8 ...

  3. Dynamics CRM The difference between UserId and InitiatingUserId in Plugin

    对于这两者的不同,MSDN的解释如下 • IExecutionContext.UserId Property: Gets the global unique identifier of the sys ...

  4. XMPP(二)-基于asmack+openfire的安卓客户端(仿QQ)的介绍以及个人心得

    关于XMPP第一篇-openfire的搭建写完后,就一直在赶本篇所要介绍的这个基于asmack+openfire的安卓客户端,费了不少精力,因为有不少同学在还在焦急的等待着(自恋了呵呵),所以紧赶慢赶 ...

  5. Effective C++ ——实现

    条款26:尽可能延后变量定义式的出现时间 当你定义一个变量的时候就要保证这个变量能够在程序中使用到,不要定义无意义的变量,这样就要求我们最好是在变量使用到的时候才做定义,因为如果一个变量定义了却不使用 ...

  6. 13 SQLiteOpenHelper SQLiteDatabase详解

    创建数据库: 1. 创建一个类继承SQLiteOpenHelper 2. 创建继承对象 new SQLiteOpenHelper() 3. 用创建的对象获取可写或者可读的SQLiteDatabase ...

  7. Fragment的事务操作&Actvity的状态丢失

    Fragment Transactions & Activity State Loss 本文翻译自Fragment Transactions & Activity State Loss ...

  8. java内存垃圾回收模型

    一.java的内存模型 介绍如下6个组成部分 1.程序计数器:一块较小内存区域,指向当前所执行的字节码.如果线程正在执行一个Java方法,这个计数器记录正在执行的虚拟机字节码指令的地址,如果执行的是N ...

  9. DBCP连接池TestOnBorrow的坑

    生产环境连接池TestOnBorrow设置为false,导致有时获取的连接不可用.分析如下: TestOnBorrow=false时,由于不检测池里连接的可用性,于是假如连接池中的连接被数据库关闭了, ...

  10. UNIX网络编程——客户/服务器程序设计示范(五)

        TCP预先派生子进程服务器程序,传递描述符 对预先派生子进程服务器程序的最后一个修改版本是只让父进程调用accept,然后把所接受的已连接套接字"传递"给某个子进程.这么做 ...