473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Convert a Java iterator loop to C++ STL?


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 '06 #1
6 2794
sa***************@gmail.com 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()) ;
}
Jan 11 '06 #2
Thanks. This is helpful.

Jan 11 '06 #3
> // 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.)
Jan 11 '06 #4
Do you have a better solution to my problem?
Thanks.

Jan 11 '06 #5
sa***************@gmail.com wrote:
Do you have a better solution to my problem?
Thanks.


Just write a plain old loop.
Jan 12 '06 #6
sa***************@gmail.com 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
Jan 12 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Scott Smedley | last post by:
Hi all, I'm trying to write a special adaptor iterator for my program. I have *almost* succeeded, though it fails under some circumstances. See the for-loop in main(). Any pointers/help...
3
by: James | last post by:
Has anyone written a utility to convert a C# form to C++.net? i.e. to convert "using System.Data" to "using namespace System::Data" etc
17
by: Allerdyce.John | last post by:
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...
5
by: Mark Stijnman | last post by:
I have a question about forward iterators and what one should do or not do with them. I'm planning on writing a container that, when all boils down to it, stores a sequence of strings. I want...
18
by: jacksu | last post by:
I have a simple program to run xpath with xerces 1_2_7 XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); XPathExpression xp = xPath.compile(strXpr);...
1
by: priyakollu | last post by:
i have developed code in jsp but the problem is i need to convert this to asp.........pleasehelp me regarding this im sending code which i've developed....with file names.......... code.jsp <%@...
27
by: Steven D'Aprano | last post by:
I thought that an iterator was any object that follows the iterator protocol, that is, it has a next() method and an __iter__() method. But I'm having problems writing a class that acts as an...
9
by: xxplod | last post by:
I am suppose to modify the Inventory Program so the application can handle multiple items. Use an array to store the items. The output should display the information one product at a time, including...
2
by: yeshello54 | last post by:
so here is my problem...in a contact manager i am trying to complete i have ran into an error..we have lots of code because we have some from class which we can use...anyways i keep getting an error...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.