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. 安卓高级5 zXing

    ZXing作者的github地址: https://github.com/zxing/zxing 这里为大家也提供一个封装好的最新的ZXing Lib: https://github.com/xuyi ...

  2. android M Launcher之LauncherModel (一)

    众所周知 LauncherModel在Launcher中所占的位置,它相当于Launcher的数据中心,Launcher的桌面以及应用程序菜单中所需的数据像 桌面小部件的信息.快捷方式信息.文件信息. ...

  3. Android 开发环境的搭建(新环境)

    最近想往Android 转型,所以又重新捡起Android学习.看了一下各位大神的文章,说的比较乱,因为版本不一样所以搭建过程也不一样,我在这里说一下最简单快捷的方式.(PS:那时候搭建环境好复杂啊, ...

  4. Xcode中lldb的REPL调试方法

    Xcode中lldb调试器有一个repl语句,可以用来模拟swift解释器的REPL行为,即Read Eval Print Loop. 在Xcode里随意打开程序,中断入调试器.在调试控制台中输入re ...

  5. Thread 方法

    Thread类的一些被Thread对象调用的方法: 1 public void start() 使该线程开始执行:Java 虚拟机调用该线程的 run 方法. 2 public void run() ...

  6. Unity3D核心技术详解

    在这里将多年游戏研发经验的积累写成一本书奉献给读者,目前已经开始预售,网址: http://www.broadview.com.cn/article/70 该书主要是将游戏中经常使用的技术给大家做了一 ...

  7. 22 Notification样式设置内部按钮点击事件

    package com.exam1ple.demo1; import android.app.Activity; import android.app.NotificationManager; imp ...

  8. IE下的deflate模式

    浏览器有一个非常有用的特性:自动解压. 在使用AJAX请求数据的时候,数据在服务器端压缩传输,在浏览器端自动解压,请求直接得到解压后的结果. 在Request Header中,一般会列出浏览器支持的压 ...

  9. Linux--NFS和DHCP服务器

     (1) 在网络中,时常需要进行文件的共享,如果都是在Linux系统下,可以使用NFS 来搭建文件服务器,达到文件共享的目的. (2) 在网络管理中,为了防止IP 冲突和盗用,有效的控制IP 资源 ...

  10. ActiveMQ安装配置及实例

    本文可作为吴水成老师,dubbo课程第21节的学习笔记. ActiveMQ的介绍及功能 参考百度 ActiveMQ的下载 https://activemq.apache.org/activemq-51 ...