STL中算法锦集(一)
STL中算法锦集(一)
文章目录
- STL中算法锦集(一)
- 一、< algorithm >
- 1.std::adjacent_find
- 2.std::all_of
- 3.std::any_of
- 4.std::binary_search
- 5.std::copy
- 6.std::copy_backward
- 7.std::copy_if
- 8.std::copy_n
- 9.std::count
- 10.std::count_if
一、< algorithm >
虽然这一部分不一定要全部掌握,但是掌握可以更快捷、方便的编程
1.std::adjacent_find
- 原型:
template <class ForwardIterator>ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last);template <class ForwardIterator, class BinaryPredicate>ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
- 参数:
- ForwardIterator first:区间的开始
- ForwardIterator last:区间的结束
- BinaryPredicate pred:
自定义比较规则
,可以是函数指针或者仿函数对象、lambda表达式
- 作用:在[first,last)这个范围中搜索
两个连续、满足pred条件、且是第一次出现的元素位置
。 - 返回值:
如果找到这两个元素对,返回first迭代器,如果没有找到这两个元素对,则返回last迭代器
- 实现:
template <class ForwardIterator>
ForwardIterator adjacent_find (ForwardIterator first,ForwardIterator last)
{if (first != last){ForwardIterator next = first; ++next;while (next != last) {// or: if (pred(*first,*next))if (*first == *next)return first;++first; ++next;}}return last;
}
- 案例:
// adjacent_find example
#include <iostream> // std::cout
#include <algorithm> // std::adjacent_find
#include <vector> // std::vectorbool myfunction (int i, int j)
{return (i==j);
}int main ()
{int myints[] = {5,20,5,30,30,20,10,10,20};std::vector<int> myvector (myints,myints+8);std::vector<int>::iterator it;// using default comparison:it = std::adjacent_find (myvector.begin(), myvector.end());if (it!=myvector.end())std::cout << "the first pair of repeated elements are: " << *it << '\n';//using predicate comparison:it = std::adjacent_find (++it, myvector.end(), myfunction);if (it != myvector.end())std::cout << "the second pair of repeated elements are: " << *it << '\n';return 0;
}Output:
the first pair of repeated elements are: 30
the second pair of repeated elements are: 10
2.std::all_of
- 原型:
template <class InputIterator, class UnaryPredicate>bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred);
- 参数:
- InputIterator first:区间的开始
- InputIterator last:区间的结束
- UnaryPredicate pred:
自定义比较规则
,可以是函数指针或者仿函数对象、lambda表达式
- 作用:
判断[first,last)区间中的所有元素是否满足pred条件
- 返回值:
如果全部满足pred条件返回true,反之有一个不满足条件返回false
- 实现:
template<class InputIterator, class UnaryPredicate>bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred)
{while (first != last) {if (!pred(*first)) return false;++first;}return true;
}
- 案例:
// all_of example
#include <iostream> // std::cout
#include <algorithm> // std::all_of
#include <array> // std::arrayint main () {std::array<int,8> foo = {3,5,7,11,13,17,19,23};if ( std::all_of(foo.begin(), foo.end(), [](int i){return i % 2;}) )std::cout << "All the elements are odd numbers.\n";return 0;
}Output:
All the elements are odd numbers.
3.std::any_of
- 原型:
template <class InputIterator, class UnaryPredicate>bool any_of (InputIterator first, InputIterator last, UnaryPredicate pred);
- 参数:
- InputIterator first:区间的开始
- InputIterator last:区间的结束
- UnaryPredicate pred:
自定义比较规则
,可以是函数指针或者仿函数对象、lambda表达式
- 作用:
判断[first,last)区间中是否有元素是否满足pred条件
- 返回值:
如果有任意一个元素满足pred条件就返回true,反之返回false
- 实现:
template<class InputIterator, class UnaryPredicate>bool any_of (InputIterator first, InputIterator last, UnaryPredicate pred)
{while (first!=last) {if (pred(*first)) return true;++first;}return false;
}
- 案例:
// any_of example
#include <iostream> // std::cout
#include <algorithm> // std::any_of
#include <array> // std::arrayint main () {std::array<int,7> foo = {0,1,-1,3,-3,5,-5};if ( std::any_of(foo.begin(), foo.end(), [](int i){return i < 0;}) )std::cout << "There are negative elements in the range.\n";return 0;
}Output:
There are negative elements in the range.
4.std::binary_search
- 原型:
template <class ForwardIterator, class T>bool binary_search (ForwardIterator first, ForwardIterator last,const T& val);template <class ForwardIterator, class T, class Compare>bool binary_search (ForwardIterator first, ForwardIterator last,const T& val, Compare comp);
- 参数:
- ForwardIterator first:区间的开始
- ForwardIterator last:区间的结束
- const T& val:待搜索的val
- Compare comp:自定义的比较器,因为元素可能是自定义的
- 作用:
二分查找区间中是否包含val元素
- 返回值:
如果包含返回true,反之返回false
- 实现:
template <class ForwardIterator, class T>bool binary_search (ForwardIterator first, ForwardIterator last, const T& val)
{first = std::lower_bound(first,last,val);return (first != last && !(val < *first));
}
- 案例:
// binary_search example
#include <iostream> // std::cout
#include <algorithm> // std::binary_search, std::sort
#include <vector> // std::vectorbool myfunction (int i,int j) { return (i < j); }int main () {int myints[] = {1,2,3,4,5,4,3,2,1};std::vector<int> v(myints,myints+9); // 1 2 3 4 5 4 3 2 1// using default comparison:std::sort (v.begin(), v.end());std::cout << "looking for a 3... ";if (std::binary_search (v.begin(), v.end(), 3))std::cout << "found!\n"; else std::cout << "not found.\n";// using myfunction as comp:std::sort (v.begin(), v.end(), myfunction);std::cout << "looking for a 6... ";if (std::binary_search (v.begin(), v.end(), 6, myfunction))std::cout << "found!\n"; else std::cout << "not found.\n";return 0;
}Output:
looking for a 3... found!
looking for a 6... not found.
5.std::copy
- 原型:
template <class InputIterator, class OutputIterator>OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result);
- 参数:
- InputIterator first:区间的开始
- InputIterator last:区间的结束
- OutputIterator result:拷贝区间的开始位置
注意[first,last)区间和result不能够重叠,否则会报错
- 作用:
把[first,last)区间当中的所有元素拷贝到result空间中
- 返回值:返回拷贝后的空间末尾位置的迭代器
- 实现:
template<class InputIterator, class OutputIterator>OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result)
{while (first != last) {*result = *first;++result;++first;}return result;
}
- 案例:
// copy algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::copy
#include <vector> // std::vectorint main () {int myints[]={10,20,30,40,50,60,70};std::vector<int> myvector (7);std::copy ( myints, myints+7, myvector.begin() );std::cout << "myvector contains:";for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';return 0;
}Output:
myvector contains: 10 20 30 40 50 60 70
6.std::copy_backward
- 原型:
template <class BidirectionalIterator1, class BidirectionalIterator2>BidirectionalIterator2 copy_backward (BidirectionalIterator1 first,BidirectionalIterator1 last,BidirectionalIterator2 result);
- 参数:
- BidirectionalIterator1 first:区间的开始
- BidirectionalIterator1 last:区间的结束
- BidirectionalIterator2 result:拷贝区间的末尾位置
注意[first,last)区间和result能够不会重叠,会报错
copy是正向copy的,而copy_backward是反向copy的,copy_backward区间如果冲突就会把前面区间的元素覆盖掉
- 作用:
把[first,last)区间当中的所有元素拷贝到result空间中
- 返回值:返回拷贝后的空间第一个位置的迭代器
- 实现:
template<class BidirectionalIterator1, class BidirectionalIterator2>BidirectionalIterator2 copy_backward ( BidirectionalIterator1 first,BidirectionalIterator1 last,BidirectionalIterator2 result )
{while (last != first) *(--result) = *(--last);return result;
}
- 案例:
// copy_backward example
#include <iostream> // std::cout
#include <algorithm> // std::copy_backward
#include <vector> // std::vectorint main () {std::vector<int> myvector;// set some values:for (int i = 1; i <= 5; i++)myvector.push_back(i * 10); // myvector: 10 20 30 40 50myvector.resize(myvector.size() + 3); // allocate space for 3 more elementsstd::copy_backward ( myvector.begin(), myvector.begin()+5, myvector.end() );std::cout << "myvector contains:";for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';return 0;
}Output:
myvector contains: 10 20 30 10 20 30 40 50
7.std::copy_if
- 原型:
template <class InputIterator, class OutputIterator, class UnaryPredicate>OutputIterator copy_if (InputIterator first, InputIterator last,OutputIterator result, UnaryPredicate pred);
- 参数:
- InputIterator first:区间的开始
- InputIterator last:区间的结束
- OutputIterator result:拷贝区间的开始位置
- UnaryPredicate pred:
自定义比较规则
,可以是函数指针或者仿函数对象、lambda表达式
- 作用:[first,last)区间中的
元素如果满足pred条件就copy到result中
- 返回值:返回拷贝后的空间末尾位置的迭代器
- 实现:
template <class InputIterator, class OutputIterator, class UnaryPredicate>OutputIterator copy_if (InputIterator first, InputIterator last,OutputIterator result, UnaryPredicate pred)
{while (first!=last) {if (pred(*first)) {*result = *first;++result;}++first;}return result;
}
- 案例:
// copy_if example
#include <iostream> // std::cout
#include <algorithm> // std::copy_if, std::distance
#include <vector> // std::vectorint main () {std::vector<int> foo = {25,15,5,-5,-15};std::vector<int> bar (foo.size());// copy only positive numbers:auto it = std::copy_if (foo.begin(), foo.end(), bar.begin(), [](int i){return !(i < 0);} );bar.resize(std::distance(bar.begin(),it)); // shrink container to new sizestd::cout << "bar contains:";for (int& x: bar) std::cout << ' ' << x;std::cout << '\n';return 0;
}Output:
bar contains: 25 15 5
8.std::copy_n
- 原型:
template <class InputIterator, class Size, class OutputIterator>OutputIterator copy_n (InputIterator first, Size n, OutputIterator result);
- 参数:
- InputIterator first:区间的开始
- Size n:拷贝元素的个数
- OutputIterator result:拷贝区间的开始位置
- 作用:
从first拷贝n个元素到result当中
- 返回值:返回拷贝区间的末尾位置的迭代器
- 实现:
template<class InputIterator, class Size, class OutputIterator>OutputIterator copy_n (InputIterator first, Size n, OutputIterator result)
{while (n > 0) {*result = *first;++result; ++first;--n;}return result;
}
- 案例:
// copy_n algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::copy
#include <vector> // std::vectorint main () {int myints[]={10,20,30,40,50,60,70};std::vector<int> myvector;myvector.resize(7); // allocate space for 7 elementsstd::copy_n ( myints, 7, myvector.begin() );std::cout << "myvector contains:";for (std::vector<int>::iterator it = myvector.begin(); it!=myvector.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';return 0;
}Output:
myvector contains: 10 20 30 40 50 60 70
9.std::count
- 原型:
template <class InputIterator, class T>typename iterator_traits<InputIterator>::difference_typecount (InputIterator first, InputIterator last, const T& val);
- 参数:
- InputIterator first:区间的开始
- InputIterator last:区间的结束
- const T& val:待计数的val
- 作用:
[first,last)区间中值为val的元素的个数
- 返回值:返回值为val的元素的个数
- 实现:
template <class InputIterator, class T>typename iterator_traits<InputIterator>::difference_typecount (InputIterator first, InputIterator last, const T& val)
{typename iterator_traits<InputIterator>::difference_type ret = 0;while (first!=last) {if (*first == val) ++ret;++first;}return ret;
}
- 案例:
// count algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::count
#include <vector> // std::vectorint main () {// counting elements in array:int myints[] = {10,20,30,30,20,10,10,20}; // 8 elementsint mycount = std::count (myints, myints+8, 10);std::cout << "10 appears " << mycount << " times.\n";// counting elements in container:std::vector<int> myvector (myints, myints+8);mycount = std::count (myvector.begin(), myvector.end(), 20);std::cout << "20 appears " << mycount << " times.\n";return 0;
}Output:
10 appears 3 times.
20 appears 3 times.
10.std::count_if
- 原型:
template <class InputIterator, class UnaryPredicate>typename iterator_traits<InputIterator>::difference_typecount_if (InputIterator first, InputIterator last, UnaryPredicate pred);
- 参数:
- InputIterator first:区间的开始
- InputIterator last:区间的结束
- UnaryPredicate pred:
自定义比较规则
,可以是函数指针或者仿函数对象、lambda表达式
- 作用:
求[first,last)区间内满足pred条件的元素的个数
- 返回值:满足pred条件的元素的个数
- 实现:
template <class InputIterator, class UnaryPredicate>typename iterator_traits<InputIterator>::difference_typecount_if (InputIterator first, InputIterator last, UnaryPredicate pred)
{typename iterator_traits<InputIterator>::difference_type ret = 0;while (first!=last) {if (pred(*first)) ++ret;++first;}return ret;
}
- 案例:
// count_if example
#include <iostream> // std::cout
#include <algorithm> // std::count_if
#include <vector> // std::vectorbool IsOdd (int i) { return ((i % 2) == 1); }int main () {std::vector<int> myvector;for (int i=1; i<10; i++) myvector.push_back(i); // myvector: 1 2 3 4 5 6 7 8 9int mycount = count_if (myvector.begin(), myvector.end(), IsOdd);std::cout << "myvector contains " << mycount << " odd values.\n";return 0;
}Output:
myvector contains 5 odd values.
STL中算法锦集(一)相关推荐
- STL中算法锦集(四)
STL中算法锦集(四) 文章目录 STL中算法锦集(四) 一.< algorithm > 1.std::is_permutation 2.std::is_sorted 3.std::is_ ...
- STL中算法锦集(三)
STL中算法锦集(三) 文章目录 STL中算法锦集(三) 一.< algorithm > 1.std::find_if 2.std::find_if_not 3.std::for_each ...
- STL中算法锦集(二)
STL中算法锦集(二) 文章目录 STL中算法锦集(二) 一.< algorithm > 1.std::equal 2.std::equal_range 3.std::fill 4.std ...
- 【数据结构与算法】二叉树基本算法锦集
二叉树结点 public class BinaryTreeNode<T> {private T data;private BinaryTreeNode<T> left;priv ...
- [C++ STL] 常用算法总结
1 概述 STL算法部分主要由头文件<algorithm>,<numeric>,<functional>组成.要使用 STL中的算法函数必须包含头文件<alg ...
- ptaa乘以b_PTA|团体程序设计天梯赛-练习题目题解锦集(C/C++)(持续更新中……)...
C++ CPP C++语言开发 PTA|团体程序设计天梯赛-练习题目题解锦集(C/C++)(持续更新中--) PTA|团体程序设计天梯赛-练习题目题解锦集(持续更新中) 实现语言:C/C++: ...
- SCPPO(十):网站发布中的问题锦集—手动发布网站
[前言] 现在做的项目是个维护性的项目,我们是第三期的末尾,最近正忙着验收.当然在验收前会有些功能需要修改,另外也会有新的需求需要实现:再加之最近有企业核对数据等等,所以网站的发布变成主要的工作之一. ...
- SCPPO(十一):网站发布中的问题锦集—ReportViewer版本问题
强烈推荐一个大神的人工智能的教程:http://www.captainbed.net/zhanghan [前言] 在小编的上篇博文<SCPPO:网站发布中的问题锦集-手动发布网站>中卖了个 ...
- 算法求解中的变量、数组与数据结构(STL 中的容器)
本质上算法都是对数据的操作,没有数据,没有存储数据的容器和组织方式,算法就是无源之水无本之木,就是巧妇也难为无米之炊.算法是演员,变量.数组.容器等就是舞台, 然后整个算法的处理流程,都是针对这些数据 ...
最新文章
- 你真的了解Python吗?这篇文章可以让你了解90%
- 谷歌大数据三篇论文标题_抖音短视频标题怎么写才能上热门?起标题的3个实操步骤,大数据分析爆款标题的套路...
- hbase/thrift/go连接失败
- BlockingQueue - LinkedBlockingQueue常用API
- elasticsearch数据备份还原
- 步长条件梯度下降算法步长和收敛条件的设置的一些看法
- 《剑指offer》面试题25——二叉树中和为某一值的路径
- TIFF图像文件格式详解——转载
- 轻轻一扭,迎来温暖柔风,告别手脚冰凉,卡蛙桌面暖风机上手
- Mbed OS STM32F429 中断方式接收 ADS1256
- JAVA初学习(三)(数组和面向对象)
- 绿色版软件怎么添加鼠标右键菜单里
- leetcode 78.不含重复元素数组的子集
- 动态表单 mysql_【mysql】动态表单应该如何设计数据库?
- IBM蓝色基因超级计算机运行Plan9操作系统
- 如何做一个基于微信在线教育学习小程序系统毕业设计毕设作品
- java fatal exception_如何解决FATAL EXCEPTION:pool-4-thread-1错误?
- 中兴通讯大数据平台在中国农业银行的应用
- Loader(浅水区)
- Ubuntu 20 仓库 http://security.ubuntu.com/ubuntu xenial-security InRelease 没有数字签名 解决