Convert a Java iterator loop to C++ STL?

Discussion in 'C++' started by samuel.y.l.cheung@gmail.com, Jan 11, 2006.

  1. Guest

    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.
    , Jan 11, 2006
    #1
    1. Advertising

  2. Alan Johnson Guest

    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()) ;
    }
    Alan Johnson, Jan 11, 2006
    #2
    1. Advertising

  3. Guest

    Thanks. This is helpful.
    , Jan 11, 2006
    #3
  4. > // 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/article.asp?p=25142&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.)
    Christopher Dearlove, Jan 11, 2006
    #4
  5. Guest

    Do you have a better solution to my problem?
    Thanks.
    , Jan 11, 2006
    #5
  6. Mark P Guest

    wrote:
    > Do you have a better solution to my problem?
    > Thanks.
    >


    Just write a plain old loop.
    Mark P, Jan 12, 2006
    #6
  7. Jeff Flinn Guest

    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
    Jeff Flinn, Jan 12, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hendrik Maryns
    Replies:
    18
    Views:
    1,404
  2. greg
    Replies:
    6
    Views:
    447
    Dietmar Kuehl
    Jul 17, 2003
  3. Replies:
    6
    Views:
    633
    Jim Langston
    Oct 30, 2005
  4. Replies:
    5
    Views:
    590
    benben
    Jan 31, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    352
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page