注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

告别迷茫

梦想与现实的差距,就是我们生活的意义。因为我们有差距,我们才会一直积累,在努力。

 
 
 

日志

 
 

21.4 UsingUnaryPredicate 判断是否为被人的整数倍 find_if  

2014-04-14 09:25:02|  分类: STL 基本的用法 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

// A structure as a unary predicate
template <typename numberType>
struct IsMultiple
{
numberType Divisor;

IsMultiple (const numberType& divisor)//初始化 初始化
{
Divisor = divisor;
}

bool operator () (const numberType& element) const
{
// Check if the dividend is a multiple of the divisor
return ((element % Divisor) == 0);
}
};

#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;

int main ()
{
vector <int> vecIntegers;
cout << "The vector contains the following sample values: ";

// Insert sample values: 25 - 31
for (int nCount = 25; nCount < 32; ++ nCount)
{
vecIntegers.push_back (nCount);
cout << nCount << ' ';
}
cout << endl << "Enter divisor (> 0): ";
int Divisor = 2;
cin >> Divisor;

// Find the first element that is a multiple of 4 in the collection
vector<int>::const_iterator iElement = find_if ( vecIntegers.begin ()
, vecIntegers.end ()
, IsMultiple<int>(Divisor) ); // replace this line by next: lambda version
// , [](int divisor){return (divisor % 4) == 0; } ); // lambda version

if (iElement != vecIntegers.end ())
{
cout << "First element in vector divisible by " << Divisor;
cout << ": " << *iElement << endl;
}

return 0;
}

21.4 UsingUnaryPredicate  判断是否为被人的整数倍 find_if - 983433479 - jet sky
 


#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;

int main ()
{
vector <int> vecIntegers;
cout << "The vector contains the following sample values: ";

// Insert sample values: 25 - 31
for (int nCount = 25; nCount < 32; ++ nCount)
{
vecIntegers.push_back (nCount);
cout << nCount << ' ';
}
cout << endl << "Enter divisor (> 0): ";
int Divisor = 2;
cin >> Divisor;

// Find the first element that is a multiple of 4 in the collection
auto iElement = find_if ( vecIntegers.begin ()
, vecIntegers.end ()
, IsMultiple<int>(Divisor) ); // replace this line by next: lambda version
// , [](int divisor){return (divisor % 4) == 0; } ); // lambda version

if (iElement != vecIntegers.end ())
{
cout << "First element in vector divisible by " << Divisor;
cout << ": " << *iElement << endl;
}

return 0;
}

lamba 表达式:

// A structure as a unary predicate
template <typename numberType>
struct IsMultiple
{
numberType Divisor;

IsMultiple (const numberType& divisor)
{
Divisor = divisor;
}

bool operator () (const numberType& element) const
{
// Check if the dividend is a multiple of the divisor
return ((element % Divisor) == 0);
}
};

#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;

int main ()
{
vector <int> vecIntegers;
cout << "The vector contains the following sample values: ";

// Insert sample values: 25 - 31
for (int nCount = 25; nCount < 32; ++ nCount)
{
vecIntegers.push_back (nCount);
cout << nCount << ' ';
}
cout << endl << "Enter divisor (> 0): ";
int Divisor = 2;
cin >> Divisor;

// Find the first element that is a multiple of 4 in the collection
auto iElement = find_if ( vecIntegers.begin ()
, vecIntegers.end ()
, [](int divisor){return (divisor % 4) == 0; } ); // lambda version

if (iElement != vecIntegers.end ())
{
cout << "First element in vector divisible by " << Divisor;
cout << ": " << *iElement << endl;
}

return 0;
}


  评论这张
 
阅读(5)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017