STL算法VS Java循环 [英] STL algorithm VS Java loop

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

问题描述




我试图比较使用STL算法之间的工作量

VS普通Java循环。


假设类Rect有2个属性:area和areaPerCent。

在Java中,我只是用一个列表写一个普通的for循环:

public static void calculateAreaPerCent(List rectList,float

containerArea){

for(Iterator iter = rectList.iterator(); iter.hasNext( );){

Rect r =(Rect)iter.next();

r.areaPerCent = r.area / containerArea;

} $ / $
}


在C ++中,建议使用STL算法而不是普通的

循环,我们需要这样做:

class doloop:public unary_function< Rect *,void>

{

public:

loopthru (float containerArea):_ containerArea(containerArea){}

void operator()(Rect * r){


r-> areaPerCent = r-> area / _containerArea;

}

私人:

_containerArea;


};


void calculateAreaPerCent(vector< ;矩形* GT;&安培; rl,float containerArea){

for_each(rl.begin(),rl.end(),doloop(containerArea));

}

在我看来,C ++ STL算法的方式需要更多的工作。这是一个公平的比较吗?有更简单的方法吗?或者我应该使用普通的C ++

进行循环?


谢谢。

解决方案

Al ************ @ gmail。 com 写道:



我试图比较使用STL算法VS /普通Java循环之间的工作量。 />
让我们说类Rect有2个属性:area和areaPerCent。

在Java中,我只是用一个列表写一个简单的for循环:
public static void calculateAreaPerCent(List rectList,float
containerArea){
for(Iterator iter = rectList.iterator(); iter.hasNext();){
Rect r =(Rect)iter。 next();
r.areaPerCent = r.area / containerArea;
}


在C ++中,建议使用STL算法代替简单的循环,我们需要这样做:
class doloop:public unary_function< Rect *,void>
{
public:
loopthru(float containerArea):_ containerArea(containerArea){}
void operator()(Rect * r){

r-> areaPerCent = r-> area / _containerArea; <私有:
_containerArea;

};

void calculateAreaPerCent(vector< Rect *>& rl,float containerArea){
for_each(rl.begin(),rl.end(),doloop(containerArea));
}


你不喜欢不需要在C ++中使用for_each,你可以使用常见的

for()循环方法。

当你有一个泛型函数时使用fore_each方法,可以和多种类型一起使用


Java没有这个选项,所以在Java中重用代码更难。

在C ++中我们有更多的选择,因此可以以一种特定的方式来实现它。

最适合特定的应用程序。

在我看来,C ++ STL算法的方式需要更多的工作。这是一个公平的比较吗?


编号因为你比较苹果和橘子。您可以使用与Java中使用的语法相同的语法,并且您可以使用更高效的代码。


有更简单的方法吗?或者我应该使用普通的C ++ for循环?



你应该使用普通的C ++循环,除非你有一个通用的

函数。


此外,你应该避免使用指针,除非你确定你有
到。

在Java中,一切基本上都是一个指针。

在C ++中,你不需要使用指针,除非你使用抽象的

类型。


如果Rect不是抽象类型,那么就不需要使用向量

指针。

vector< Rect> vRect;


如果Rect是抽象类型,那么你应该使用智能指针,

而不是原始虚拟指针。

vector< boost :: shared_ptr< Rect> > vRect;



vector< cow_ptr< Rect> > vRect;


请参阅以下关于牛指针的链接:
http://code.axter.com/cow_ptr.h
http://code.axter.com/copy_ptr.h

并查看shared_ptr的boost库。





Axter写道:

你不需要使用for_each in C ++,你可以使用常见的
for()循环方法。
当你有一个泛型函数时,可以使用fore_each方法,它可以与多种类型一起使用。


为什么?

我没有看到任何关系...

Java没有这个选项,因此,在Java中重用代码更加困难。




Java 5知道泛型,顺便说一句......

< blockquote class =post_quotes>在我看来,C ++ STL算法的方式需要更多的工作。这是一个公平的比较吗?



没有。因为你比较苹果和橘子。您几乎可以使用与Java相同的语法,并且您拥有更高效的代码。




更高效?我怀疑for_each(开始,结束,行动)归结为

除了(因为i =开始;我!=结束; ++ i)行动(* i);


Markus


Axter写道:

vector< boost :: shared_ptr< Rect> > vRect;




Boost还有一个指针容器。你可能会发现更多的图书馆比一个shared_ptr的矢量更合适,这取决于你的程序需要多少灵活性。

http://www.boost.org/libs/ptr_contai...container。 HTML


-Alan


Hi,

I am trying to compare the amount of work between using STL algorithm
VS a plain Java loop.

Let''s say the class Rect has 2 attributes: area, and areaPerCent.

In Java, I just write a plain for loop with a list:
public static void calculateAreaPerCent(List rectList, float
containerArea) {
for (Iterator iter = rectList.iterator(); iter.hasNext();) {
Rect r = (Rect) iter.next();
r.areaPerCent = r.area / containerArea;
}
}

And in C++, it is recommded to use STL algorithm instead of a plain
loop, we need to do this:
class doloop : public unary_function<Rect*, void>
{
public:
loopthru(float containerArea) : _containerArea(containerArea) { }

void operator() (Rect* r) {

r->areaPerCent = r->area /_containerArea;
}
private:
_containerArea;

};

void calculateAreaPerCent(vector<Rect*>& rl, float containerArea) {
for_each (rl.begin(), rl.end(), doloop(containerArea));
}

It seems to me the C++ STL algorithm way needs more work. Is that a
fair comparision? Is there a simpler way? Or I should use a plain C++
for loop ?

Thank you.

解决方案

Al************@gmail.com wrote:

Hi,

I am trying to compare the amount of work between using STL algorithm
VS a plain Java loop.

Let''s say the class Rect has 2 attributes: area, and areaPerCent.

In Java, I just write a plain for loop with a list:
public static void calculateAreaPerCent(List rectList, float
containerArea) {
for (Iterator iter = rectList.iterator(); iter.hasNext();) {
Rect r = (Rect) iter.next();
r.areaPerCent = r.area / containerArea;
}
}

And in C++, it is recommded to use STL algorithm instead of a plain
loop, we need to do this:
class doloop : public unary_function<Rect*, void>
{
public:
loopthru(float containerArea) : _containerArea(containerArea) { }

void operator() (Rect* r) {

r->areaPerCent = r->area /_containerArea;
}
private:
_containerArea;

};

void calculateAreaPerCent(vector<Rect*>& rl, float containerArea) {
for_each (rl.begin(), rl.end(), doloop(containerArea));
}
You don''t need to use a for_each in C++, and you can use the common
for() loop method.
Use the fore_each method when you have a generic function, that can be
used with multiple types.
Java doesn''t have this option, so it''s harder to reuse code in Java.
In C++ we have more options, and therefore can do it in a manner that
is best for a paticular application.

It seems to me the C++ STL algorithm way needs more work. Is that a
fair comparision?
No. Since you''re comparring apples and oranges. You can use nearly
the same syntax used in Java, and you have more efficient code.

Is there a simpler way? Or I should use a plain C++ for loop ?


You should be using the plain C++ loop, unless you have a generic
function.

Moreover, you should avoid using pointers unless you''re sure you have
to.
In Java, everything is basically a pointer.
In C++, you don''t need to use pointers, unless you''re using abstract
types.

If Rect is not an abstract type, than there''s no need to use a vector
of pointers.
vector<Rect> vRect;

If Rect is an abstract type, than you should use a smart pointer,
instead of a raw dummy pointer.
vector<boost::shared_ptr<Rect> > vRect;
or
vector<cow_ptr<Rect> > vRect;

See following link for cow pointer:
http://code.axter.com/cow_ptr.h
http://code.axter.com/copy_ptr.h

And check out the boost library for shared_ptr.


Hi

Axter wrote:

You don''t need to use a for_each in C++, and you can use the common
for() loop method.
Use the fore_each method when you have a generic function, that can be
used with multiple types.
Why?
I don''t see any relation...
Java doesn''t have this option, so it''s harder to reuse code in Java.



Java 5 knows generics, btw...

It seems to me the C++ STL algorithm way needs more work. Is that a
fair comparision?



No. Since you''re comparring apples and oranges. You can use nearly
the same syntax used in Java, and you have more efficient code.



More efficient? I doubt that for_each(begin,end,action) boils down to
anything else but for(For i=begin; i!=end; ++i) action(*i);

Markus


Axter wrote:

vector<boost::shared_ptr<Rect> > vRect;



Boost also has a "pointer container" library that you might find more
suitable than a vector of shared_ptr, depending on how much flexibility
your program needs.

http://www.boost.org/libs/ptr_contai...container.html

-Alan


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

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