算法一:非变动性算法

 

 

 

1、非变动性算法既不改变元素的值,也不改变元素的顺序

 

for_each() 对每个元素执行某操作

count() 返回元素个数

count()_if() 返回满足某一条件的元素个数

min_element() 返回最小值元素

max_element() 返回最大值元素

find() 搜索等于某个值的第一个元素

find_if() 搜索满足某个条件的第一个元素

search_n() 搜索具有某特性的第一段n个连续元素

search() 搜索某个子区间第一次出现位置

find_end() 搜索某个子区间最后一次出现位置

find_first_of() 搜索等于"某数个值之一"的第一元素

adjacent_find() 搜索连续两个相等的元素

equal() 判断两区间是否相等 

mismatch()  返回两个序列的各组对应元素中,第一对不相等元素 

 

 

// 78cpp.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <list>
using namespace std;

 

//利用函数对象
class GreaterObj
{
public:
 GreaterObj(int number):number_(number){}

 bool operator()(int n){

  return n>number_;
 }
protected:
private:
 int number_;
};

 

//打印元素
void printElement(int n1)
{
 cout<<n1<<' ';
}
//找出大于3的元素位置
bool greater_than_3(int n2)
{
 return n2>3;
}
int _tmain(int argc, _TCHAR* argv[])
{
 int bb[]={1,2,3,4,5};
 vector<int> v(bb,bb+5);
 vector<int>::const_iterator it;
 list<int> li;
 list<int>::const_iterator ltList;
 for (it=v.begin();it!=v.end();it++)
 {
  cout<<*it<<" ";
  //cout<<endl;
 }
 cout<<endl;

 //for_each使用
 for_each(v.begin(),v.end(),printElement);
 cout<<endl;

 for (it=v.begin();it!=v.end();it++)
 {
  cout<<*it<<" ";
  //cout<<endl;
 }
 cout<<endl;

 //min_element使用
 it=min_element(v.begin(),v.end());
 if (it!=v.end())
 {
  cout<<*it<<endl;
 }
 //max_element使用
 it=max_element(v.begin(),v.end());
 if (it!=v.end())
 {
  cout<<*it<<endl;
 }
 //find使用
 it=find(v.begin(),v.end(),6);
 if (it!=v.end())
 {
  cout<<it-v.begin()<<endl;
 }else{
  cout<<"not found"<<endl;
 }
 //find_if使用
 it=find_if(v.begin(),v.end(),greater_than_3);
 if (it!=v.end())
 {
  cout<<*it<<endl;
 }else{
  cout<<"not found"<<endl;
 }


 int j=2;
 for (j=2;j<4;j++)
 {
  li.push_back(j);
 }
 for_each(li.begin(),li.end(),printElement);
 cout<<endl;


  //search使用
 vector<int>::iterator result;

 result=search(v.begin(),v.end(),li.begin(),li.end());
 if (result==v.end())
 {
  cout<<"not match...of li in v";
 }
 else
 {
  cout<<"match...of li in v"<<endl;
  cout<<result-v.begin();
 }

int a[]={1,2,3,4,5};

cout<<count_if(a,a+5,GreaterObj(3))<<endl;利用函数对象,统计大于3的元素个数

 

 

 return 0;
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值