Chinaunix首页 | 论坛 | 博客
  • 博客访问: 580172
  • 博文数量: 136
  • 博客积分: 893
  • 博客等级: 中士
  • 技术积分: 1001
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-31 09:18
个人简介

生命可以终止,梦想不会!

文章分类

全部博文(136)

文章存档

2016年(4)

2015年(2)

2014年(5)

2013年(7)

2012年(118)

分类: Windows平台

2015-01-08 10:36:12

先说#include "algostuff.h" 函数

点击(此处)折叠或打开

  1. #ifndef ALGOSTUFF_H
  2. #define ALGOSTUFF_H
  3.   
  4. #include <iostream>
  5. #include <vector>
  6. #include <list>
  7. #include <deque>
  8. #include <set>
  9. #include <map>
  10. #include <string>
  11. #include <algorithm>
  12. #include <functional>
  13. #include <numeric>
  14.   
  15. //print all the elements
  16. template <class T>
  17. inline void PRINT_ELEMNTS(const T &col,const char *optcstr = " ")
  18. {
  19.     typename T::const_iterator pos;
  20.     cout << optcstr;
  21.     for(pos = col.begin();pos != col.end();++pos)
  22.         cout << *pos << " ";
  23.     cout << endl;
  24. }
  25.   
  26. //insert values from first to last into the collection
  27. template <class T>
  28. inline void INSERT_ELEMENTS(T &col,int first,int last)
  29. {
  30.     for(int i = first;i <= last;++i)
  31.         col.insert(col.end(),i);
  32. }
  33.   
  34. #endif

find算法

点击(此处)折叠或打开

  1. template<class InputIterator, class T>
  2.   InputIterator find ( InputIterator first, InputIterator last, const T& value )
  3.   {
  4.     for ( ;first!=last; first++) if ( *first==value ) break;
  5.     return first;
  6.   }

返回区间[first,end)中第一个值等于value的元素的位置。

如果没有找到匹配元素,则返回end。


点击(此处)折叠或打开

  1. list<string> Fruit;
  2. list<string>::iterator FruitIterator;

  3. Fruit.push_back("Apple");
  4. Fruit.push_back("Pineapple");
  5. Fruit.push_back("Star Apple");

  6. FruitIterator = find (Fruit.begin(), Fruit.end(),"Pineapple");
  7.  
  8. //如果没找到,则返回end
  9.     if (FruitIterator == Fruit.end()) {
  10.        cout << "Fruit not found in list" << endl;
  11.     }
  12.     else {
  13.        cout<< *FruitIterator <<endl;
  14.     }

find_if()算法

template<class InputIterator, class Predicate>  
InputIterator find_if ( InputIterator first, InputIterator last, Predicate pred )  
{  
    for ( ; first!=last ; first++ ) if ( pred(*first) ) break;  
    return first;  

它在区间[first,end)中搜寻使一元判断式pred为true的第一个元素。

如果没找到,返回end。


点击(此处)折叠或打开

  1. #include "algostuff.h"
  2.   
  3. using namespace std;
  4.   
  5. int main()
  6. {
  7.     vector<int> intVec;
  8.   
  9.     INSERT_ELEMENTS(intVec,1,9);
  10.   
  11.     vector<int>::iterator pos;
  12.     pos = find_if(intVec.begin(),intVec.end(),
  13.         not1(bind2nd(modulus<int>(),3)));
  14.   
  15.     if(pos != intVec.end())
  16.         cout << "The value divided by 3 exists,and the first value's position is " <<
  17.         distance(intVec.begin(),pos) + 1 << endl;
  18.     else
  19.         cout << "The value divided by 3 not found!" << endl;
  20. }





阅读(1907) | 评论(0) | 转发(0) |
0

上一篇:Windows核心编程学习一

下一篇:博客搬家

给主人留下些什么吧!~~