C++STL之查找子序列的算法
2018-03-24 15:35
591 查看
//----------------------------------------------------------------------------------------
// Desc: STL search() used in vector container and struct data
// Author: spring brother
// Data: 2018.3.24
// Copyright (C) 2018 spring brother
//----------------------------------------------------------------------------------------
/*
查找子序列的算法
search(beg1,end1,beg2,end2)
search(beg1,end1,beg2,end2,binaryPred)
返回第二个输入范围(子序列)在第一个输入范围中第一次出现的位置。如果未找到子序列,则返回end1。
find_first_of(beg1,end1,beg2,end2)
find_first_of(beg1,end1,beg2,end2,binaryPred)
返回一个迭代器,指向第二个输入范围中任意元素在第一个范围中首次出现的位置。如果未找到匹配元素,则返回end1。
find_end(beg1,end1,beg2,end2)
find_end(beg1,end1,beg2,end2,binaryPred)
类似search,但是返回的是最后一次出现的位置。如果第二个输入范围为空,或者在第一个输入范围中未找到它,则返回end1。
*/
#include "stdafx.h"
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
bool isEqual(int &i, int &j) {
return i == j / 2;
}
int main()
{
int arrIn[6] = { 1,2,4,4,4,5 };
int arrSub[3] = { 2,4,4 };
vector<int> vecIn(arrIn, arrIn + 6);
vector<int> vecSub(arrSub, arrSub + 3);
vector<int>::iterator it = search(vecIn.begin(), vecIn.end(), vecSub.begin(), vecSub.end()); //search
if (it != vecIn.end()) {
cout << "search = " << *it << endl;
}
it = search(vecIn.begin(), vecIn.end(), vecSub.begin(), vecSub.end()-1,isEqual); //search,传入仿函数
if (it != vecIn.end()) {
cout << "search = " << *it << endl;
}
it = search(vecIn.begin(), vecIn.end(), vecSub.begin(), vecSub.end()-1, [](int &i, int &j) {return i == j / 2; }); //search,传入仿函数
if (it != vecIn.end()) {
cout << "search = " << *it << endl;
}
int arrSub_1[3] = { 7,8,5 };
vector<int> vecSub_1(arrSub_1, arrSub_1 + 3);
it = find_first_of(vecIn.begin(), vecIn.end(), vecSub_1.begin(), vecSub_1.end()); //find_first_of
if (it != vecIn.end()) {
cout << "find_first_of = " << *it << endl;
}
it = find_first_of(vecIn.begin(), vecIn.end(), vecSub_1.begin(), vecSub_1.end() - 1, isEqual); //find_first_of,传入仿函数
if (it != vecIn.end()) {
cout << "find_first_of = " << *it << endl;
}
it = find_first_of(vecIn.begin(), vecIn.end(), vecSub_1.begin(), vecSub_1.end() - 1, [](int &i, int &j) {return i == j / 7; }); //find_first_of,传入仿函数
if (it != vecIn.end()) {
cout << "find_first_of = " << *it << endl;
}
it = find_end(vecIn.begin(), vecIn.end(), vecSub.begin(), vecSub.end()); //find_end
if (it != vecIn.end()) {
cout << "find_end = " << *it << endl;
}
it = find_end(vecIn.begin(), vecIn.end(), vecSub.begin(), vecSub.end() - 1, isEqual); //find_end,传入仿函数
if (it != vecIn.end()) {
cout << "find_end = " << *it << endl;
}
it = find_end(vecIn.begin(), vecIn.end(), vecSub.begin(), vecSub.end() - 1, [](int &i, int &j) {return i == j / 2; }); //find_end,传入仿函数
if (it != vecIn.end()) {
cout << "find_end = " << *it << endl;
}
return 0;
}
// Desc: STL search() used in vector container and struct data
// Author: spring brother
// Data: 2018.3.24
// Copyright (C) 2018 spring brother
//----------------------------------------------------------------------------------------
/*
查找子序列的算法
search(beg1,end1,beg2,end2)
search(beg1,end1,beg2,end2,binaryPred)
返回第二个输入范围(子序列)在第一个输入范围中第一次出现的位置。如果未找到子序列,则返回end1。
find_first_of(beg1,end1,beg2,end2)
find_first_of(beg1,end1,beg2,end2,binaryPred)
返回一个迭代器,指向第二个输入范围中任意元素在第一个范围中首次出现的位置。如果未找到匹配元素,则返回end1。
find_end(beg1,end1,beg2,end2)
find_end(beg1,end1,beg2,end2,binaryPred)
类似search,但是返回的是最后一次出现的位置。如果第二个输入范围为空,或者在第一个输入范围中未找到它,则返回end1。
*/
#include "stdafx.h"
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
bool isEqual(int &i, int &j) {
return i == j / 2;
}
int main()
{
int arrIn[6] = { 1,2,4,4,4,5 };
int arrSub[3] = { 2,4,4 };
vector<int> vecIn(arrIn, arrIn + 6);
vector<int> vecSub(arrSub, arrSub + 3);
vector<int>::iterator it = search(vecIn.begin(), vecIn.end(), vecSub.begin(), vecSub.end()); //search
if (it != vecIn.end()) {
cout << "search = " << *it << endl;
}
it = search(vecIn.begin(), vecIn.end(), vecSub.begin(), vecSub.end()-1,isEqual); //search,传入仿函数
if (it != vecIn.end()) {
cout << "search = " << *it << endl;
}
it = search(vecIn.begin(), vecIn.end(), vecSub.begin(), vecSub.end()-1, [](int &i, int &j) {return i == j / 2; }); //search,传入仿函数
if (it != vecIn.end()) {
cout << "search = " << *it << endl;
}
int arrSub_1[3] = { 7,8,5 };
vector<int> vecSub_1(arrSub_1, arrSub_1 + 3);
it = find_first_of(vecIn.begin(), vecIn.end(), vecSub_1.begin(), vecSub_1.end()); //find_first_of
if (it != vecIn.end()) {
cout << "find_first_of = " << *it << endl;
}
it = find_first_of(vecIn.begin(), vecIn.end(), vecSub_1.begin(), vecSub_1.end() - 1, isEqual); //find_first_of,传入仿函数
if (it != vecIn.end()) {
cout << "find_first_of = " << *it << endl;
}
it = find_first_of(vecIn.begin(), vecIn.end(), vecSub_1.begin(), vecSub_1.end() - 1, [](int &i, int &j) {return i == j / 7; }); //find_first_of,传入仿函数
if (it != vecIn.end()) {
cout << "find_first_of = " << *it << endl;
}
it = find_end(vecIn.begin(), vecIn.end(), vecSub.begin(), vecSub.end()); //find_end
if (it != vecIn.end()) {
cout << "find_end = " << *it << endl;
}
it = find_end(vecIn.begin(), vecIn.end(), vecSub.begin(), vecSub.end() - 1, isEqual); //find_end,传入仿函数
if (it != vecIn.end()) {
cout << "find_end = " << *it << endl;
}
it = find_end(vecIn.begin(), vecIn.end(), vecSub.begin(), vecSub.end() - 1, [](int &i, int &j) {return i == j / 2; }); //find_end,传入仿函数
if (it != vecIn.end()) {
cout << "find_end = " << *it << endl;
}
return 0;
}
相关文章推荐
- C++ STL 算法精选之查找篇
- C++STL之查找重复值的算法
- 不会改变操作对象内容的STL算法(元素查找,序列查找,统计元素个数,比较区间,找最值)
- C++ STL 算法精选之查找篇
- C++提高5 STL算法 :查找,统计,排序,拷贝,替换,算术,集合 |STL 案例:学校演讲比赛介绍
- stl的算法(一):对序列进行只读操作(查找、搜索等)
- c++之STL(13) STL 算法 - 查找算法(2)search_n() search_n(b, e, c, v) search_n(b, e, c, v, p)
- C++ STL 算法:查找算法(6) binary_search、includes
- C++ Primer 学习笔记_45_STL实践与分析(19)--泛型算法的结构
- STL算法------查找6(二分查找,包含查找)
- C++——STL所有算法介绍
- C++实现查找中位数的O(N)算法和Kmin算法
- C++ STL 中有关于for_each() 和 transform() 算法
- C++ STL源码学习(基本算法篇)
- [置顶] MoreWindows 微软认证专家博客目录(白话算法,C++ STL,windows编程)
- 【C++研发面试笔记】21. 常用算法-STL中常用算法函数
- C++每日一练(STL算法——sort)
- 查找有序序列中 重复/不存在 的数算法
- 【C++ STL】算法 <algorithm>中各种算法解析
- C++中STL常用算法概要