STL中算法锦集(二)

文章目录

  • STL中算法锦集(二)
    • 一、< algorithm >
      • 1.std::equal
      • 2.std::equal_range
      • 3.std::fill
      • 4.std::fill_n
      • 5.std::find
      • 6.std::find_end
      • 7.std::find_first_of

一、< algorithm >

1.std::equal

  • 原型:
template <class InputIterator1, class InputIterator2>bool equal (InputIterator1 first1, InputIterator1 last1,InputIterator2 first2);template <class InputIterator1, class InputIterator2, class BinaryPredicate>bool equal (InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, BinaryPredicate pred);
  • 参数:
  • InputIterator1 first1:第一个区间的开始
  • InputIterator1 last1:第一个区间的结束
  • InputIterator2 first2:第二个区间的开始
  • BinaryPredicate pred:自定义比较规则,可以是函数指针或者仿函数对象、lambda表达式
  • 注意第一个区间的长度如果比第二个区间的长度长,那么肯定返回false;
  • 反之第一个区间的长度如果比第二个区间的长度短,程序正常判断的
  • 作用:[first1,last1)区间中的每个元素和first2区间中的对应元素是否满足pred条件
  • 返回值:如果第一个区间中的每个元素和第二个区间中的对应元素都满足pred条件,返回true,反之返回false
  • 实现:
template <class InputIterator1, class InputIterator2>bool equal ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 )
{while (first1!=last1) {// or: if (!pred(*first1,*first2))if (!(*first1 == *first2))return false;++first1; ++first2;}return true;
}
  • 案例:
// equal algorithm example
#include <iostream>     // std::cout
#include <algorithm>    // std::equal
#include <vector>       // std::vectorbool mypredicate (int i, int j) {return (i==j);
}int main () {int myints[] = {20,40,60,80,100};               //   myints: 20 40 60 80 100std::vector<int>myvector (myints,myints+5);     // myvector: 20 40 60 80 100// using default comparison:if ( std::equal (myvector.begin(), myvector.end(), myints) )std::cout << "The contents of both sequences are equal.\n";elsestd::cout << "The contents of both sequences differ.\n";myvector[3]=81;   // myvector: 20 40 60 81 100// using predicate comparison:if ( std::equal (myvector.begin(), myvector.end(), myints, mypredicate) )std::cout << "The contents of both sequences are equal.\n";elsestd::cout << "The contents of both sequences differ.\n";return 0;
}Output:
The contents of both sequences are equal.
The contents of both sequence differ.

2.std::equal_range

  • 原型:
template <class ForwardIterator, class T>pair<ForwardIterator,ForwardIterator>equal_range (ForwardIterator first, ForwardIterator last, const T& val);template <class ForwardIterator, class T, class Compare>pair<ForwardIterator,ForwardIterator>equal_range (ForwardIterator first, ForwardIterator last, const T& val,Compare comp);
  • 参数:
  • ForwardIterator first:区间的开始
  • ForwardIterator last:区间的结束
  • const T& val:待比较的值
  • Compare comp:自定义比较规则
  • 需要排序
  • 作用:求[first,last)区间内值为val的范围,左闭右开区间
  • 返回值:返回值为一个pair的键值对,每个元素都是迭代器,[begin,end):代表值为val的开始位置和结束位置的下一个位置
  • 实现:
template <class ForwardIterator, class T>pair<ForwardIterator,ForwardIterator>equal_range (ForwardIterator first, ForwardIterator last, const T& val)
{ForwardIterator it = std::lower_bound (first,last,val);return std::make_pair ( it, std::upper_bound(it,last,val) );
}
  • 案例:
// equal_range example
// equal_range example
#include <iostream>     // std::cout
#include <algorithm>    // std::equal_range, std::sort
#include <vector>       // std::vectorbool mygreater (int i,int j) { return (i>j); }int main () {int myints[] = {10,20,30,30,20,10,10,20};std::vector<int> v(myints,myints+8);  // 10 20 30 30 20 10 10 20std::pair<std::vector<int>::iterator,std::vector<int>::iterator> bounds;// using default comparison:std::sort (v.begin(), v.end());// 10 10 10 20 20 20 30 30bounds=std::equal_range (v.begin(), v.end(), 20);// using "mygreater" as comp:std::sort (v.begin(), v.end(), mygreater);// 30 30 20 20 20 10 10 10bounds=std::equal_range (v.begin(), v.end(), 20, mygreater); //  std::cout << "bounds at positions " << (bounds.first - v.begin());std::cout << " and " << (bounds.second - v.begin()) << '\n';return 0;
}Output:
bounds at positions 2 and 5

3.std::fill

  • 原型:
template <class ForwardIterator, class T>void fill (ForwardIterator first, ForwardIterator last, const T& val);
  • 参数:
  • ForwardIterator first:区间的开始
  • ForwardIterator last:区间的结束
  • const T& val:填充的val
  • 作用:把[first,last)区间用val值填充
  • 返回值:没有返回值
  • 实现:
template <class ForwardIterator, class T>void fill (ForwardIterator first, ForwardIterator last, const T& val)
{while (first != last) {*first = val;++first;}
}
  • 案例:
// fill algorithm example
#include <iostream>     // std::cout
#include <algorithm>    // std::fill
#include <vector>       // std::vectorint main () {std::vector<int> myvector (8);// myvector: 0 0 0 0 0 0 0 0std::fill (myvector.begin(),myvector.begin()+4,5);   // myvector: 5 5 5 5 0 0 0 0std::fill (myvector.begin()+3,myvector.end()-2,8);   // myvector: 5 5 5 8 8 8 0 0std::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: 5 5 5 8 8 8 0 0

4.std::fill_n

  • 原型:
//98
template <class OutputIterator, class Size, class T>void fill_n (OutputIterator first, Size n, const T& val);//11
template <class OutputIterator, class Size, class T>OutputIterator fill_n (OutputIterator first, Size n, const T& val);
  • 参数:
  • OutputIterator first:区间的开始
  • Size n
  • const T& val
  • 作用:从first位置开始填充n个val值
  • 返回值:98:没有返回值。11:返回填充完成后位置的下一个位置
  • 实现:
template <class OutputIterator, class Size, class T>OutputIterator fill_n (OutputIterator first, Size n, const T& val)
{while (n > 0) {*first = val;++first; --n;}return first; // since C++11
}
  • 案例:
// fill_n example
#include <iostream>     // std::cout
#include <algorithm>    // std::fill_n
#include <vector>       // std::vectorint main () {std::vector<int> myvector (8,10);        // myvector: 10 10 10 10 10 10 10 10std::fill_n (myvector.begin(),4,20);     // myvector: 20 20 20 20 10 10 10 10std::fill_n (myvector.begin()+3,3,33);   // myvector: 20 20 20 33 33 33 10 10std::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: 20 20 20 33 33 33 10 10

5.std::find

  • 原型:
template <class InputIterator, class T>InputIterator find (InputIterator first, InputIterator last, const T& val);
  • 参数:
  • InputIterator first:区间的开始
  • InputIterator last:区间的结束
  • const T& val:待查找的值val
  • 作用:[first,last)区间内查找第一个值为val的元素的位置
  • 返回值:返回第一个值为val的元素的位置
  • 实现:
template<class InputIterator, class T>InputIterator find (InputIterator first, InputIterator last, const T& val)
{while (first!=last) {if (*first==val) return first;++first;}return last;
}
  • 案例:
// find example
#include <iostream>     // std::cout
#include <algorithm>    // std::find
#include <vector>       // std::vectorint main () {// using std::find with array and pointer:int myints[] = { 10, 20, 30, 40 };int * p;p = std::find (myints, myints+4, 30);if (p != myints+4)std::cout << "Element found in myints: " << *p << '\n';elsestd::cout << "Element not found in myints\n";// using std::find with vector and iterator:std::vector<int> myvector (myints,myints+4);std::vector<int>::iterator it;it = find (myvector.begin(), myvector.end(), 30);if (it != myvector.end())std::cout << "Element found in myvector: " << *it << '\n';elsestd::cout << "Element not found in myvector\n";return 0;
}Output:
Element found in myints: 30
Element found in myvector: 30

6.std::find_end

  • 原型:
template <class ForwardIterator1, class ForwardIterator2>ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1,ForwardIterator2 first2, ForwardIterator2 last2);template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1,ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
  • 参数:
  • ForwardIterator1 first1:区间1的开始
  • ForwardIterator1 last1:区间1的结束
  • ForwardIterator2 first2:区间2的开始
  • ForwardIterator2 last2:区间2的结束
  • BinaryPredicate pred:自定义比较规则,可以是函数指针或者仿函数对象、lambda表达式
  • 作用:在[first1,last1)区间的查找满足[first2,last2)区间中的所有元素
  • 返回值:如果[first1,last1)区间查找到[first2,last2)区间中的所有元素,返回[first2,last2)区间在[first1,last1)区间最后一个满足条件的开始位置
  • 实现:
template<class ForwardIterator1, class ForwardIterator2>ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1,ForwardIterator2 first2, ForwardIterator2 last2)
{if (first2==last2) return last1;  // specified in C++11ForwardIterator1 ret = last1;while (first1 != last1){ForwardIterator1 it1 = first1;ForwardIterator2 it2 = first2;// or: while (pred(*it1,*it2))while (*it1 == *it2) {++it1; ++it2;if (it2==last2) { ret=first1; break; }if (it1==last1) return ret;}++first1;}return ret;
}
  • 案例:
// find_end example
#include <iostream>     // std::cout
#include <algorithm>    // std::find_end
#include <vector>       // std::vectorbool myfunction (int i, int j) {return (i==j);
}int main () {int myints[] = {1,2,3,4,5,1,2,3,4,5};std::vector<int> haystack (myints,myints+10);int needle1[] = {1,2,3};// using default comparison:std::vector<int>::iterator it;it = std::find_end (haystack.begin(), haystack.end(), needle1, needle1+3);if (it!=haystack.end())std::cout << "needle1 last found at position " << (it-haystack.begin()) << '\n';int needle2[] = {4,5,1};// using predicate comparison:it = std::find_end (haystack.begin(), haystack.end(), needle2, needle2+3, myfunction);if (it!=haystack.end())std::cout << "needle2 last found at position " << (it-haystack.begin()) << '\n';return 0;
}Output:
needle1 found at position 5
needle2 found at position 3

7.std::find_first_of

  • 原型:
//98:
template <class ForwardIterator1, class ForwardIterator2>ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1,ForwardIterator2 first2, ForwardIterator2 last2);template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1,ForwardIterator2 first2, ForwardIterator2 last2,BinaryPredicate pred);//11:
template <class InputIterator, class ForwardIterator>InputIterator find_first_of (InputIterator first1, InputIterator last1,ForwardIterator first2, ForwardIterator last2);template <class InputIterator, class ForwardIterator, class BinaryPredicate>InputIterator find_first_of (InputIterator first1, InputIterator last1,ForwardIterator first2, ForwardIterator last2,BinaryPredicate pred);
  • 参数:
  • InputIterator first1:区间的开始
  • InputIterator last1:区间的结束
  • ForwardIterator first2:区间的开始
  • ForwardIterator last2:区间的结束
  • BinaryPredicate pred:自定义比较规则,可以是函数指针或者仿函数对象、lambda表达式
  • 作用:在[first1,last1)区间内找[first2,last2)区间内任意一个元素出现的位置
  • 返回值:如果在[first1,last1)区间内找[first2,last2)区间内任意一个元素返回该位置的迭代器
  • 实现:
template<class InputIterator, class ForwardIterator>InputIterator find_first_of ( InputIterator first1, InputIterator last1,ForwardIterator first2, ForwardIterator last2)
{while (first1!=last1) {for (ForwardIterator it=first2; it!=last2; ++it) {// or: if (pred(*it,*first))if (*it==*first1)          return first1;}++first1;}return last1;
}
  • 案例:
// find_first_of example
#include <iostream>     // std::cout
#include <algorithm>    // std::find_first_of
#include <vector>       // std::vector
#include <cctype>       // std::tolowerbool comp_case_insensitive (char c1, char c2) {return (std::tolower(c1)==std::tolower(c2));
}int main () {int mychars[] = {'a','b','c','A','B','C'};std::vector<char> haystack (mychars,mychars+6);std::vector<char>::iterator it;int needle[] = {'A','B','C'};// using default comparison:it = find_first_of (haystack.begin(), haystack.end(), needle, needle+3);if (it!=haystack.end())std::cout << "The first match is: " << *it << '\n';// using predicate comparison:it = find_first_of (haystack.begin(), haystack.end(),needle, needle+3, comp_case_insensitive);if (it!=haystack.end())std::cout << "The first match is: " << *it << '\n';return 0;
}Output:
The first match is: A
The first match is: a

STL中算法锦集(二)相关推荐

  1. STL中算法锦集(四)

    STL中算法锦集(四) 文章目录 STL中算法锦集(四) 一.< algorithm > 1.std::is_permutation 2.std::is_sorted 3.std::is_ ...

  2. STL中算法锦集(三)

    STL中算法锦集(三) 文章目录 STL中算法锦集(三) 一.< algorithm > 1.std::find_if 2.std::find_if_not 3.std::for_each ...

  3. STL中算法锦集(一)

    STL中算法锦集(一) 文章目录 STL中算法锦集(一) 一.< algorithm > 1.std::adjacent_find 2.std::all_of 3.std::any_of ...

  4. 报错锦集二“nginx: [emerg] unknown directive “set_real_ip_from“ in /usr/local/nginx/conf/nginx.conf:50 ngi

    报错锦集二"nginx: [emerg] unknown directive "set_real_ip_from" in /usr/local/nginx/conf/ng ...

  5. 字节跳动面试锦集(二):项目HR高频面试总结

    1.项目开发中遇到的最大的一个难题和挑战,你是如何解决的.(95% 会问到) 说说你开发最大的优势点(95% 会问到) 你为什么会离开上家公司 你的缺点是什么? 你能给公司带来什么效益? 你对未来的职 ...

  6. Spring boot锦集(二):整合邮件发送的四种方法 | 纯文本的邮件、带有图片的邮件、带Html的邮件、带附件的邮件(很详细)

    前言 邮件发送,听着很神秘,然而对于Spring Boot来说,这个功能已被集成好,只需引入spring-boot-starter-mail依赖后,少量代码即可实现大部分邮件发送需求. 本文以异常教程 ...

  7. 我的Go+语言初体验——ubuntu安装Go+环境问题锦集(二)

    欢迎大家参与[我的Go+语言初体验]活动: 活动地址:[https://bbs.csdn.net/topics/603464006?utm_source=1594742339] 本文为大家带来[Go+ ...

  8. 【数据结构与算法】二叉树基本算法锦集

    二叉树结点 public class BinaryTreeNode<T> {private T data;private BinaryTreeNode<T> left;priv ...

  9. [C++ STL] 常用算法总结

    1 概述 STL算法部分主要由头文件<algorithm>,<numeric>,<functional>组成.要使用 STL中的算法函数必须包含头文件<alg ...

最新文章

  1. Python学习之continue
  2. python操作excel表格-Python学习—对excel表格的操作
  3. php模拟请求get请求,php模拟get请求方法总结
  4. Office Web App2013 在线查看PDF文件
  5. VSTO学习笔记(四)从SharePoint 2010中下载文件
  6. 向量的 加,减,乘 运算_2
  7. 正式发布 .Net2.0 大文件上传服务器控件
  8. python3.9新特性_Python 3.9正式版,新特性提前一睹为快
  9. 理论到实践带你了解情感分析、信息抽取、搜索推荐等NLP相关任务
  10. iOS图形学(四):iOS中的绘图框架
  11. PBFT(一):过程
  12. windows10任务栏无响应解决方案
  13. Mac OS 开机密码重置
  14. HTML的relative与absolute区别
  15. flex fills
  16. 可视化 | 用Python分析近5000个旅游景点,告诉你假期应该去哪玩
  17. 苹果手机上滑动会卡顿_苹果手机Safari浏览器下滑动卡顿的问题
  18. 岁月温柔-20 妈妈在省医院第一天
  19. [Scrapy教学9]一定要懂的Scrapy框架结合Gmail寄送爬取资料附件秘诀
  20. 广度优先搜索算法带图详解

热门文章

  1. python寻找列表最大值最小值及其下标
  2. 关于github里readme编辑的方法
  3. Bootstrap手机网站开发案例
  4. Arduino开发板制作
  5. SQL 备份还原单个表
  6. CodeForces - 1355E Restorer Distance(三分)
  7. CodeForces - 1312D Count the Arrays(组合数学)
  8. POJ - 3347 Kadj Squares(思维+几何)
  9. 牛客 - 小A的回文串(Manacher模板题)
  10. HDU - 2255 奔小康赚大钱(二分图最大权匹配+KM)