STL算法示例 [英] STL Algorithm Example

查看:92
本文介绍了STL算法示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在教自己如何使用STL的算法,因此每次我有一个

循环我试图将其转换为算法调用...


然而,我被这一个所困:


for(vector< double> :: iterator raValIt = raVal.begin();

raValIt!= raVal.end();

++ raValIt)

{

if(* raValIt > = upperBorder)

{

* raValIt - = 360.0;

}

}


这只是一个循环最好的情况吗?我不会为了这么简单的功能而花费一些函数...但是对于标准算法选项来说似乎太复杂了。
br />
有什么想法吗?


干杯,

罗斯

I''m teaching myself how to use STL''s algorithms, so every time I have a
loop I''m attempting to convert it into an algorithm call...

However, I''m stumpted by this one:

for (vector<double>::iterator raValIt = raVal.begin();
raValIt != raVal.end();
++raValIt)
{
if (*raValIt >= upperBorder)
{
*raValIt -= 360.0;
}
}

Is this just a case where you''re better off with a loop? I''m not going
to bother with functor for such a simple function... but it seems too
complex for the standard algorithm options.

Any ideas?

Cheers,
Ross

推荐答案

2005-11-03,zexpe< us **** @ zexpe.freeserve.co.uk>写道:
On 2005-11-03, zexpe <us****@zexpe.freeserve.co.uk> wrote:
我在教自己如何使用STL的算法,所以每次我有一个循环我试图将它转换为算法
打电话......

然而,我被这个人所困扰:

for(vector< double> :: iterator raValIt = raVal.begin();
raValIt!= raVal.end();
++ raValIt)
{
if(* raValIt> = upperBorder)
{
* raValIt - = 360.0;
}
}

这只是一个循环最好的情况吗?我不打算用仿函数来完成这么简单的功能......
但对于标准算法选项来说似乎太复杂了。
I''m teaching myself how to use STL''s algorithms, so every time
I have a loop I''m attempting to convert it into an algorithm
call...

However, I''m stumpted by this one:

for (vector<double>::iterator raValIt = raVal.begin();
raValIt != raVal.end();
++raValIt)
{
if (*raValIt >= upperBorder)
{
*raValIt -= 360.0;
}
}

Is this just a case where you''re better off with a loop? I''m
not going to bother with functor for such a simple function...
but it seems too complex for the standard algorithm options.




它是std :: transform的应用程序。但是如果你不想写b
,那就忘了它。有时候少数特别是简单的标准算法(转换,for_each)不值得给你带来麻烦。


另一方面,还有boost :: lambda允许你使用

标准仿函数即可轻松生成仿函数
和适配器。


-

Neil Cerutti



It''s an application of std::transform. But if you don''t want to
write a functor then forget it. Sometimes the few especially
simple-minded standard algorithms (transform, for_each) aren''t
worth the trouble.

On the other hand, there''s boost::lambda which allows you to
build functors on the fly that you can''t easily produce with the
standard functors and adaptors.

--
Neil Cerutti


zexpe写道:
我在教自己如何使用STL的算法,所以每次我有一个
循环我试图将它转换为算法调用...

for(vector< double> :: iterator raValIt = raVal.begin();
raValIt!= raVal.end() ;
++ raValIt)
{
if(* raValIt> = upperBorder)
{
* raValIt - = 360.0;
} }

这只是y的情况你最好用循环?我不打算使用仿函数来完成这么简单的功能......但对于标准算法选项来说似乎太复杂了。
I''m teaching myself how to use STL''s algorithms, so every time I have a
loop I''m attempting to convert it into an algorithm call...

However, I''m stumpted by this one:

for (vector<double>::iterator raValIt = raVal.begin();
raValIt != raVal.end();
++raValIt)
{
if (*raValIt >= upperBorder)
{
*raValIt -= 360.0;
}
}

Is this just a case where you''re better off with a loop? I''m not going
to bother with functor for such a simple function... but it seems too
complex for the standard algorithm options.




#include< functional>

#include< iostream>


模板<类InputIterator,类OutputIterator,类UnaryProc,

class UnaryPredicate>

void transform_if(InputIterator begin,InputIterator end,

OutputIterator out,UnaryProc op,UnaryPredicate pred)

{

while(开始!=结束)

{

if(pred(* begin))

* out = op(* begin);


++开始;

++ out;

}

}


void f(std :: vector< double>& v,double upperBorder)

{

transform_if(

v.begin(),v.end(),v.begin(),

std :: bind2nd(std :: minus< double>() ,360.0),

std :: bind2nd(std :: greater_equal< double>(),upperBorder));

}

Jonathan



# include <functional>
# include <iostream>

template <class InputIterator, class OutputIterator, class UnaryProc,
class UnaryPredicate>
void transform_if(InputIterator begin, InputIterator end,
OutputIterator out, UnaryProc op, UnaryPredicate pred)
{
while (begin != end)
{
if (pred(*begin))
*out = op(*begin);

++begin;
++out;
}
}

void f(std::vector<double> &v, double upperBorder)
{
transform_if(
v.begin(), v.end(), v.begin(),
std::bind2nd(std::minus<double>(), 360.0),
std::bind2nd(std::greater_equal<double>(), upperBorder));
}
Jonathan


Jonathan Mcdougall写道:
Jonathan Mcdougall wrote:
void f(std :: vector< double> & v,double upperBorder)
{/ / / / / / / / / / / / / / / :: minus< double>(),360.0),
std :: bind2nd(std :: greater_equal< double>(),upperBorder));
}
void f(std::vector<double> &v, double upperBorder)
{
transform_if(
v.begin(), v.end(), v.begin(),
std::bind2nd(std::minus<double>(), 360.0),
std::bind2nd(std::greater_equal<double>(), upperBorder));
}




什么是transform_if?并且你认为这对于一个平均C ++程序员(可能以后可能需要使用你的代码)而言比

显式循环更具可读性吗?


我赞成STL算法,但不幸的是有很多

的情况,其中STL算法不是很有用,除非我们引入

闭包。


-


Valentin Samko - http://www.valentinsamko.com



What''s transform_if ? And do you think this is more readable to an
average C++ programmer (who may have to work with your code later) than
the explicit loop?

I am all in favour of STL algorithms, but unfortunately there are many
cases where STL algorithms are not very usefull unless we introduce
closures.

--

Valentin Samko - http://www.valentinsamko.com


这篇关于STL算法示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆