Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Convert a Java iterator loop to C++ STL?

Reply
Thread Tools

Convert a Java iterator loop to C++ STL?

 
 
samuel.y.l.cheung@gmail.com
Guest
Posts: n/a
 
      01-11-2006

Hi,

I am trying to convert a Java iterator loop to C++ STL? the loop looks
like this:

public static boolean func (List aList) {
int minX = 0
for (Iterator iter = aList.listIterator(1); iter.hasNext() {
A a = (A) iter.next();

if (! closeEnough(minX, a.x) ) {
return true;
} else {
minX = a.x;
}
}
return false;
}

I am reading the "Effecitive STL" book, it said i should use STL
algorithm instead of writing my loop if possible. I was thinking of
using for_each(). But in this case, i need to update a local variable
'minX' and stop the iteration if certain condition is met. Should I
use for_each()? or I write my own iterator loop?

Thank you for any idea.

 
Reply With Quote
 
 
 
 
Alan Johnson
Guest
Posts: n/a
 
      01-11-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi,
>
> I am trying to convert a Java iterator loop to C++ STL? the loop looks
> like this:
>
> public static boolean func (List aList) {
> int minX = 0
> for (Iterator iter = aList.listIterator(1); iter.hasNext() {
> A a = (A) iter.next();
>
> if (! closeEnough(minX, a.x) ) {
> return true;
> } else {
> minX = a.x;
> }
> }
> return false;
> }
>
> I am reading the "Effecitive STL" book, it said i should use STL
> algorithm instead of writing my loop if possible. I was thinking of
> using for_each(). But in this case, i need to update a local variable
> 'minX' and stop the iteration if certain condition is met. Should I
> use for_each()? or I write my own iterator loop?
>
> Thank you for any idea.
>


I think the algorithm you want is "find_if". You can then write your
own predicate. For example, you might have a class named "closeEnough"
to act as your predicate:

#include <functional>

class closeEnough : public std::unary_function<bool, int>
{
private :
int minX ;
public :
closeEnough() : minX(0) {}

bool operator()(int x)
{
// Your logic for the "closeEnough" function goes here.
// Note your ability to change minX as you like.
return true ;
}
} ;


Then your function above would reduce to something like the following
(forgive me if I got some of the logic backwards, but this is the
general idea):

#include <algorithm>
#include <list>

bool func(std::list<int> aList)
{
std::list<int>::iterator i ;
closeEnough cefunc ;

i = std::find_if(aList.begin(), aList.end(), cefunc) ;
return (i == aList.end()) ;
}
 
Reply With Quote
 
 
 
 
samuel.y.l.cheung@gmail.com
Guest
Posts: n/a
 
      01-11-2006
Thanks. This is helpful.

 
Reply With Quote
 
Christopher Dearlove
Guest
Posts: n/a
 
      01-11-2006
> // Note your ability to change minX as you like.
> return true ;


Not necessarily. Changing state in a predicate is a dangerous business, see

http://www.informit.com/articles/art...&seqNum=4&rl=1

(Here the order of find_if is guaranteed, so you are more likely to get
away with it. But it's a dodgy practice at best, and likely to teach bad
habits.)


 
Reply With Quote
 
samuel.y.l.cheung@gmail.com
Guest
Posts: n/a
 
      01-11-2006
Do you have a better solution to my problem?
Thanks.

 
Reply With Quote
 
Mark P
Guest
Posts: n/a
 
      01-12-2006
(E-Mail Removed) wrote:
> Do you have a better solution to my problem?
> Thanks.
>


Just write a plain old loop.
 
Reply With Quote
 
Jeff Flinn
Guest
Posts: n/a
 
      01-12-2006
(E-Mail Removed) wrote:
> Do you have a better solution to my problem?
> Thanks.


Depending on just what CloseEnough does, you may just be able to use
min_element as show below. I'm not sure by your example as to whether the
min value is really needed.

#include <algorithm>
#include <list>

bool func( const std::list<int>& aList )
{
typedef std::list<int>::const_iterator tCItr;

int lMinValue = 0;

tCItr i = std::min_element( aList.begin(), aList.end(), cefunc );

if( i != aList.end() )
{
lMinValue = *i;

return false;
}
else
{
return true;
}
}

Jeff


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
What makes an iterator an iterator? Steven D'Aprano Python 28 04-20-2007 03:34 AM
Difference between Java iterator and iterator in Gang of Four Hendrik Maryns Java 18 12-22-2005 05:14 AM
How to convert from std::list<T*>::iterator to std::list<const T*>::iterator? PengYu.UT@gmail.com C++ 6 10-30-2005 03:31 AM
Iterator doubts, Decision on Iterator usage greg C++ 6 07-17-2003 01:26 PM



Advertisments