A
Allerdyce.John
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.
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.