C++STL之查找重复值的算法
2018-03-24 14:53
253 查看
//----------------------------------------------------------------------------------------
// Desc: STL adjacent_find() used in struct data
// Author: spring brother
// Data: 2018.3.24
// Copyright (C) 2018 spring brother
//----------------------------------------------------------------------------------------
/*
查找重复值的算法
adjacent_find(beg,end)
adjacent_find(beg,end,binaryPred)
返回指向第一对相邻重复元素的迭代器。如果序列中无相邻重复元素则返回end。
search_n(beg,end,count,val)
search_n(beg,end,count,val,binayrPred)
返回一个迭代器,从此位置开始有count个相等元素。如果序列中不存在这样的子序列,则返回end。
注意:adjacent_find可用于查找拥有倍数关系的相邻元素,而search_n只能用于查找数值相等的相邻元素。
另外,search_n的仿函数及匿名函数的参数不能为引用类型。
*/
#include "stdafx.h"
#include <algorithm>
#include <iostream>
using namespace std;
bool isEqual(int &i,int &j) {
return 2*i == j;
}
bool ifEqual(int i, int j) { //search_n的仿函数及匿名函数的参数不能为引用类型
return i == j;
}
int main()
{
int arrIn[6] = { 1,2,4,4,4,5 };
int *temp = adjacent_find(arrIn, arrIn + 6); //adjacent_find
if (temp != arrIn + 6) {
cout << "adjacent_find = " << *temp << endl;
}
temp = adjacent_find(arrIn, arrIn + 6, isEqual); //adjacent_find,传入仿函数
if (temp != arrIn + 6) {
cout << "adjacent_find = " << *temp << endl;
}
temp = adjacent_find(arrIn, arrIn + 6, [](int &i, int &j) {return 2 * i == j; }); //adjacent_find,传入匿名函数
if (temp != arrIn + 6) {
cout << "adjacent_find = " << *temp << endl;
}
int count = 3;
int val = 4;
temp = search_n(arrIn, arrIn + 6, count, val); //search_n
if (temp != arrIn + 6) {
cout << "search_n = " << *temp << endl;
}
temp = search_n(arrIn, arrIn + 6, count, val, ifEqual); //search_n,传入仿函数
if (temp != arrIn + 6) {
cout << "search_n = " << *temp << endl;
}
temp = search_n(arrIn, arrIn + 6, count, val, [](int i,int j) {return i == j; }); //search_n,传入匿名函数
if (temp != arrIn + 6) {
cout << "search_n = " << *temp << endl;
}
return 0;
}
// Desc: STL adjacent_find() used in struct data
// Author: spring brother
// Data: 2018.3.24
// Copyright (C) 2018 spring brother
//----------------------------------------------------------------------------------------
/*
查找重复值的算法
adjacent_find(beg,end)
adjacent_find(beg,end,binaryPred)
返回指向第一对相邻重复元素的迭代器。如果序列中无相邻重复元素则返回end。
search_n(beg,end,count,val)
search_n(beg,end,count,val,binayrPred)
返回一个迭代器,从此位置开始有count个相等元素。如果序列中不存在这样的子序列,则返回end。
注意:adjacent_find可用于查找拥有倍数关系的相邻元素,而search_n只能用于查找数值相等的相邻元素。
另外,search_n的仿函数及匿名函数的参数不能为引用类型。
*/
#include "stdafx.h"
#include <algorithm>
#include <iostream>
using namespace std;
bool isEqual(int &i,int &j) {
return 2*i == j;
}
bool ifEqual(int i, int j) { //search_n的仿函数及匿名函数的参数不能为引用类型
return i == j;
}
int main()
{
int arrIn[6] = { 1,2,4,4,4,5 };
int *temp = adjacent_find(arrIn, arrIn + 6); //adjacent_find
if (temp != arrIn + 6) {
cout << "adjacent_find = " << *temp << endl;
}
temp = adjacent_find(arrIn, arrIn + 6, isEqual); //adjacent_find,传入仿函数
if (temp != arrIn + 6) {
cout << "adjacent_find = " << *temp << endl;
}
temp = adjacent_find(arrIn, arrIn + 6, [](int &i, int &j) {return 2 * i == j; }); //adjacent_find,传入匿名函数
if (temp != arrIn + 6) {
cout << "adjacent_find = " << *temp << endl;
}
int count = 3;
int val = 4;
temp = search_n(arrIn, arrIn + 6, count, val); //search_n
if (temp != arrIn + 6) {
cout << "search_n = " << *temp << endl;
}
temp = search_n(arrIn, arrIn + 6, count, val, ifEqual); //search_n,传入仿函数
if (temp != arrIn + 6) {
cout << "search_n = " << *temp << endl;
}
temp = search_n(arrIn, arrIn + 6, count, val, [](int i,int j) {return i == j; }); //search_n,传入匿名函数
if (temp != arrIn + 6) {
cout << "search_n = " << *temp << endl;
}
return 0;
}
相关文章推荐
- C++ 算法 查找旋转数组中的最小值 允许重复元素
- C++ STL 算法精选之查找篇
- C++ STL 算法精选之查找篇
- C++STL之查找子序列的算法
- C/C++面试之算法系列--时间复杂度为o(N)查找1至N-1构成的a[N]重复元素
- C++提高5 STL算法 :查找,统计,排序,拷贝,替换,算术,集合 |STL 案例:学校演讲比赛介绍
- 给一个字符串,例如”ababc",要求返回"ab"。因为"ab”连续重复出现且最长。用C/C++语言写一个函数完成该算法,给出复杂度
- C++——STL所有算法介绍
- C++精华(STL)学习笔记之算法
- c++ 标准模板库 STL 算法之 for_each 函数的使用用法详解
- C++ STL 常用算法
- 二分查找算法在C/C++程序中的应用示例
- 转:c++STL算法整理
- java小算法—查找字符串中第一次重复的字符
- 算法笔记--c++STL
- 从零开始学C++之STL(八):函数对象、 函数对象与容器、函数对象与算法
- C++ Primer 学习笔记_45_STL实践与分析(19)--泛型算法的结构
- C++——STL所有算法介绍 http://blog.csdn.net/shinehoo/article/details/5736155
- 【转】C++ 常用的STL查找函数方法
- C++ - 部分STL容器如何去除重复元素