473,385 Members | 2,004 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,385 software developers and data experts.

how to reverse find_if?

JDT
Hi,

The following find_if works fine to me except that I want the internal
individual comparison performed backward (from m+5, m+4, ..., to m). I
can use reverse() to reverse the array first but that introduces
overhead (a copy of the whole array). It seems that I can use a reverse
iterator but the array m is not a standard STL container such as a
vector. Is it possible? Your help is much appreciated.

float m[6];
float *p = find_if(m, m+6, bind2nd(greater_equal<float>(), 14.5));

Tony
Mar 16 '07 #1
10 7564
JDT wrote:
Hi,

The following find_if works fine to me except that I want the internal
individual comparison performed backward (from m+5, m+4, ..., to m). I
can use reverse() to reverse the array first but that introduces
overhead (a copy of the whole array). It seems that I can use a reverse
iterator but the array m is not a standard STL container such as a
vector. Is it possible? Your help is much appreciated.

float m[6];
float *p = find_if(m, m+6, bind2nd(greater_equal<float>(), 14.5));
I think that you can use std::reverse_iterator with a pointer. See 24.1.1

i.e.:

typedef std::reverse_iterator<float *rfloat_iter;
rfloat_iter p =
std::find_if(rfloat_iter(m+6), rfloat_iter(m),
std::bind2nd(greater_equal<float>(), 14.5));
Mar 16 '07 #2
JDT
Hi Red,

Thanks for your help. First of all, what do you mean by 24.1.1?
Second, I am testing your code. To check if find_if finds something,
should I use "p == rfloat_iter(m+6)" or "p == rfloat_iter(m)"? Besides,
is the range between m+5 and m-1?

Your code has some compile errors. I appreciate if somebody can show me
what's the correct syntax (because I am not familiar with this regard).
Thanks for any further help.

Tony

red floyd wrote:
JDT wrote:
>Hi,

The following find_if works fine to me except that I want the internal
individual comparison performed backward (from m+5, m+4, ..., to m).
I can use reverse() to reverse the array first but that introduces
overhead (a copy of the whole array). It seems that I can use a
reverse iterator but the array m is not a standard STL container such
as a vector. Is it possible? Your help is much appreciated.

float m[6];
float *p = find_if(m, m+6, bind2nd(greater_equal<float>(), 14.5));

I think that you can use std::reverse_iterator with a pointer. See 24.1.1

i.e.:

typedef std::reverse_iterator<float *rfloat_iter;
rfloat_iter p =
std::find_if(rfloat_iter(m+6), rfloat_iter(m),
std::bind2nd(greater_equal<float>(), 14.5));
Mar 16 '07 #3
JDT wrote:
red floyd wrote:
>I think that you can use std::reverse_iterator with a pointer. See 24.1.1

typedef std::reverse_iterator<float *rfloat_iter;
rfloat_iter p =
std::find_if(rfloat_iter(m+6), rfloat_iter(m),
std::bind2nd(greater_equal<float>(), 14.5));
Thanks for your help. First of all, what do you mean by 24.1.1?
Section 24.1.1 of the ISO C++ Standard (ISO/IEC 14882:2003).
I am testing your code. To check if find_if finds something, should I
use "p == rfloat_iter(m+6)" or "p == rfloat_iter(m)"?
The latter.
Besides, is the range between m+5 and m-1?
Sort of. See 24.1.1 for more details.
Your code has some compile errors. I appreciate if somebody can show me
what's the correct syntax (because I am not familiar with this regard).
I'm not surprised, I wrote it off the top of my head. I'm sure that one
of the other more knowledgeable types here can help you more than me.

Also, as a matter of etiquette, please try not to top-post (posting all
your text above what you're replying to) -- it's frowned on in this
newsgroup. Instead, intersperse your replies with the text you're
referring to (as I did here), or after the text.
Mar 16 '07 #4
red floyd wrote:
JDT wrote:
>red floyd wrote:
>>I think that you can use std::reverse_iterator with a pointer. See
24.1.1

typedef std::reverse_iterator<float *rfloat_iter;
rfloat_iter p =
std::find_if(rfloat_iter(m+6), rfloat_iter(m),
std::bind2nd(greater_equal<float>(), 14.5));
>Thanks for your help. First of all, what do you mean by 24.1.1?

Section 24.1.1 of the ISO C++ Standard (ISO/IEC 14882:2003).
>I am testing your code. To check if find_if finds something, should I
use "p == rfloat_iter(m+6)" or "p == rfloat_iter(m)"?
The latter.
>Besides, is the range between m+5 and m-1?

Sort of. See 24.1.1 for more details.
>Your code has some compile errors. I appreciate if somebody can show
me what's the correct syntax (because I am not familiar with this
regard).
You need to #include <iterator>
>
I'm not surprised, I wrote it off the top of my head. I'm sure that one
of the other more knowledgeable types here can help you more than me.

Also, as a matter of etiquette, please try not to top-post (posting all
your text above what you're replying to) -- it's frowned on in this
newsgroup. Instead, intersperse your replies with the text you're
referring to (as I did here), or after the text.
you might try this:

#include <iterator>
typedef std::reverse_iterator<float *rfloat_iter;
const rfloat_iter rbegin = rfloat_iter(m+6);
const rfloat_iter rend = rfloat_iter(m);
rfloat_iter p =
std::find_if(rbegin, rend,
std::bind2nd(std::greater_equal<float>(),
14.5));
if (p == r_end)
/* not found */;
Mar 16 '07 #5
JDT
red floyd wrote:
red floyd wrote:
>JDT wrote:
>>red floyd wrote:

I think that you can use std::reverse_iterator with a pointer. See
24.1.1

typedef std::reverse_iterator<float *rfloat_iter;
rfloat_iter p =
std::find_if(rfloat_iter(m+6), rfloat_iter(m),
std::bind2nd(greater_equal<float>(), 14.5));

>>Thanks for your help. First of all, what do you mean by 24.1.1?


Section 24.1.1 of the ISO C++ Standard (ISO/IEC 14882:2003).
>>I am testing your code. To check if find_if finds something, should
I use "p == rfloat_iter(m+6)" or "p == rfloat_iter(m)"?

The latter.
>>Besides, is the range between m+5 and m-1?


Sort of. See 24.1.1 for more details.
>>Your code has some compile errors. I appreciate if somebody can show
me what's the correct syntax (because I am not familiar with this
regard).


You need to #include <iterator>
>>
I'm not surprised, I wrote it off the top of my head. I'm sure that
one of the other more knowledgeable types here can help you more than me.

Also, as a matter of etiquette, please try not to top-post (posting
all your text above what you're replying to) -- it's frowned on in
this newsgroup. Instead, intersperse your replies with the text
you're referring to (as I did here), or after the text.


you might try this:

#include <iterator>
typedef std::reverse_iterator<float *rfloat_iter;
const rfloat_iter rbegin = rfloat_iter(m+6);
const rfloat_iter rend = rfloat_iter(m);
rfloat_iter p =
std::find_if(rbegin, rend,
std::bind2nd(std::greater_equal<float>(),
14.5));
if (p == r_end)
/* not found */;

Hi Red,

The compiler doesn't like the following line so the typedef statement fails:
reverse_iterator<float *>

Thanks for your help.

Tony


Mar 16 '07 #6
"JDT" <jd*******@yahoo.comwrote in message
news:vp*****************@newssvr27.news.prodigy.ne t...
red floyd wrote:
>red floyd wrote:
>>JDT wrote:

red floyd wrote:

I think that you can use std::reverse_iterator with a pointer. See
24.1.1
>
typedef std::reverse_iterator<float *rfloat_iter;
rfloat_iter p =
std::find_if(rfloat_iter(m+6), rfloat_iter(m),
std::bind2nd(greater_equal<float>(), 14.5));
Thanks for your help. First of all, what do you mean by 24.1.1?
Section 24.1.1 of the ISO C++ Standard (ISO/IEC 14882:2003).

I am testing your code. To check if find_if finds something, should I
use "p == rfloat_iter(m+6)" or "p == rfloat_iter(m)"?

The latter.

Besides, is the range between m+5 and m-1?
Sort of. See 24.1.1 for more details.

Your code has some compile errors. I appreciate if somebody can show
me what's the correct syntax (because I am not familiar with this
regard).


You need to #include <iterator>
>>>
I'm not surprised, I wrote it off the top of my head. I'm sure that one
of the other more knowledgeable types here can help you more than me.

Also, as a matter of etiquette, please try not to top-post (posting all
your text above what you're replying to) -- it's frowned on in this
newsgroup. Instead, intersperse your replies with the text you're
referring to (as I did here), or after the text.


you might try this:

#include <iterator>
typedef std::reverse_iterator<float *rfloat_iter;
const rfloat_iter rbegin = rfloat_iter(m+6);
const rfloat_iter rend = rfloat_iter(m);
rfloat_iter p =
std::find_if(rbegin, rend,
std::bind2nd(std::greater_equal<float>(),
14.5));
if (p == r_end)
/* not found */;


Hi Red,

The compiler doesn't like the following line so the typedef statement
fails:
reverse_iterator<float *>

Thanks for your help.

Tony
I *think*, but am not positive, he meant something like:
std::vector<float*>::reverse_iterator
Mar 16 '07 #7
JDT
Jim Langston wrote:
"JDT" <jd*******@yahoo.comwrote in message
news:vp*****************@newssvr27.news.prodigy.ne t...
>>red floyd wrote:
>>>red floyd wrote:
JDT wrote:
>red floyd wrote:
>
>
>>I think that you can use std::reverse_iterator with a pointer. See
>>24.1.1
>>
>>typedef std::reverse_iterator<float *rfloat_iter;
>>rfloat_iter p =
> std::find_if(rfloat_iter(m+6), rfloat_iter(m),
> std::bind2nd(greater_equal<float>(), 14.5));
>Thanks for your help. First of all, what do you mean by 24.1.1?
Section 24.1.1 of the ISO C++ Standard (ISO/IEC 14882:2003).
>I am testing your code. To check if find_if finds something, should I
>use "p == rfloat_iter(m+6)" or "p == rfloat_iter(m)"?

The latter.
>Besides, is the range between m+5 and m-1?
Sort of. See 24.1.1 for more details.
>Your code has some compile errors. I appreciate if somebody can show
>me what's the correct syntax (because I am not familiar with this
>regard).
You need to #include <iterator>

I'm not surprised, I wrote it off the top of my head. I'm sure that one
of the other more knowledgeable types here can help you more than me.

Also, as a matter of etiquette, please try not to top-post (posting all
your text above what you're replying to) -- it's frowned on in this
newsgroup. Instead, intersperse your replies with the text you're
referring to (as I did here), or after the text.
you might try this:

#include <iterator>
typedef std::reverse_iterator<float *rfloat_iter;
const rfloat_iter rbegin = rfloat_iter(m+6);
const rfloat_iter rend = rfloat_iter(m);
rfloat_iter p =
std::find_if(rbegin, rend,
std::bind2nd(std::greater_equal<float>(),
14.5));
if (p == r_end)
/* not found */;


Hi Red,

The compiler doesn't like the following line so the typedef statement
fails:
reverse_iterator<float *>

Thanks for your help.

Tony


I *think*, but am not positive, he meant something like:
std::vector<float*>::reverse_iterator

But can we mix a vector with a regular array?

Tony
Mar 16 '07 #8
Jim Langston wrote:
"JDT" <jd*******@yahoo.comwrote in message
news:vp*****************@newssvr27.news.prodigy.ne t...
>red floyd wrote:
>>red floyd wrote:

JDT wrote:

red floyd wrote:
>
>I think that you can use std::reverse_iterator with a pointer. See
>24.1.1
>>
>typedef std::reverse_iterator<float *rfloat_iter;
>rfloat_iter p =
> std::find_if(rfloat_iter(m+6), rfloat_iter(m),
> std::bind2nd(greater_equal<float>(), 14.5));
Thanks for your help. First of all, what do you mean by 24.1.1?
Section 24.1.1 of the ISO C++ Standard (ISO/IEC 14882:2003).

I am testing your code. To check if find_if finds something, should I
use "p == rfloat_iter(m+6)" or "p == rfloat_iter(m)"?

The latter.

Besides, is the range between m+5 and m-1?
Sort of. See 24.1.1 for more details.

Your code has some compile errors. I appreciate if somebody can show
me what's the correct syntax (because I am not familiar with this
regard).
You need to #include <iterator>
I'm not surprised, I wrote it off the top of my head. I'm sure that
one of the other more knowledgeable types here can help you more than
me.

Also, as a matter of etiquette, please try not to top-post (posting all
your text above what you're replying to) -- it's frowned on in this
newsgroup. Instead, intersperse your replies with the text you're
referring to (as I did here), or after the text.
you might try this:

#include <iterator>
typedef std::reverse_iterator<float *rfloat_iter;
const rfloat_iter rbegin = rfloat_iter(m+6);
const rfloat_iter rend = rfloat_iter(m);
rfloat_iter p =
std::find_if(rbegin, rend,
std::bind2nd(std::greater_equal<float>(),
14.5));
if (p == r_end)
/* not found */;


Hi Red,

The compiler doesn't like the following line so the typedef statement
fails:
reverse_iterator<float *>

Thanks for your help.

Tony

I *think*, but am not positive, he meant something like:
std::vector<float*>::reverse_iterator
No, he did not: the whole point is to turn pointers (used as iterators into
a raw array) into reverse_iterators into that same array. There is no
vector in any of this.
To the OP:

The following (which is the code from red floyd) compiles on g++ and Comeau.

#include <iterator>
#include <algorithm>
#include <functional>

typedef std::reverse_iterator<float *rfloat_iter;

int main ( void ) {
float m[6];
const rfloat_iter rbegin = rfloat_iter(m+6);
const rfloat_iter rend = rfloat_iter(m);
rfloat_iter p =
std::find_if(rbegin, rend,
std::bind2nd(std::greater_equal<float>(),
14.5));
}

If you still can't get the code to compile, please post a short but complete
program that demonstrates the problem.
Best

Kai-Uwe Bux
Mar 17 '07 #9
Kai-Uwe Bux wrote:
Jim Langston wrote:
>"JDT" <jd*******@yahoo.comwrote in message
news:vp*****************@newssvr27.news.prodigy.n et...
>>red floyd wrote:
red floyd wrote:

JDT wrote:
>
>red floyd wrote:
>>
>>I think that you can use std::reverse_iterator with a pointer. See
>>24.1.1
>>>
>>typedef std::reverse_iterator<float *rfloat_iter;
>>rfloat_iter p =
>> std::find_if(rfloat_iter(m+6), rfloat_iter(m),
>> std::bind2nd(greater_equal<float>(), 14.5));
>
>Thanks for your help. First of all, what do you mean by 24.1.1?
>
Section 24.1.1 of the ISO C++ Standard (ISO/IEC 14882:2003).
>
>I am testing your code. To check if find_if finds something, should I
>use "p == rfloat_iter(m+6)" or "p == rfloat_iter(m)"?
The latter.
>
>Besides, is the range between m+5 and m-1?
>
Sort of. See 24.1.1 for more details.
>
>Your code has some compile errors. I appreciate if somebody can show
>me what's the correct syntax (because I am not familiar with this
>regard).

You need to #include <iterator>

I'm not surprised, I wrote it off the top of my head. I'm sure that
one of the other more knowledgeable types here can help you more than
me.
>
Also, as a matter of etiquette, please try not to top-post (posting all
your text above what you're replying to) -- it's frowned on in this
newsgroup. Instead, intersperse your replies with the text you're
referring to (as I did here), or after the text.

you might try this:

#include <iterator>
typedef std::reverse_iterator<float *rfloat_iter;
const rfloat_iter rbegin = rfloat_iter(m+6);
const rfloat_iter rend = rfloat_iter(m);
rfloat_iter p =
std::find_if(rbegin, rend,
std::bind2nd(std::greater_equal<float>(),
14.5));
if (p == r_end)
/* not found */;
Hi Red,

The compiler doesn't like the following line so the typedef statement
fails:
reverse_iterator<float *>

Thanks for your help.

Tony
I *think*, but am not positive, he meant something like:
std::vector<float*>::reverse_iterator

No, he did not: the whole point is to turn pointers (used as iterators into
a raw array) into reverse_iterators into that same array. There is no
vector in any of this.
To the OP:

The following (which is the code from red floyd) compiles on g++ and Comeau.

#include <iterator>
#include <algorithm>
#include <functional>

typedef std::reverse_iterator<float *rfloat_iter;

int main ( void ) {
float m[6];
const rfloat_iter rbegin = rfloat_iter(m+6);
const rfloat_iter rend = rfloat_iter(m);
rfloat_iter p =
std::find_if(rbegin, rend,
std::bind2nd(std::greater_equal<float>(),
14.5));
}

If you still can't get the code to compile, please post a short but complete
program that demonstrates the problem.

Thanks, Kai-Uwe. I suspect the OP's problem is that he didn't put the
std:: in front of the reverse_iterator in the typedef.
Mar 17 '07 #10
Kai-Uwe Bux wrote:
Jim Langston wrote:
>"JDT" <jd*******@yahoo.comwrote in message
news:vp*****************@newssvr27.news.prodigy.n et...
>>red floyd wrote:
red floyd wrote:

JDT wrote:
>
>red floyd wrote:
>>
>>I think that you can use std::reverse_iterator with a pointer. See
>>24.1.1
>>>
>>typedef std::reverse_iterator<float *rfloat_iter;
>>rfloat_iter p =
>> std::find_if(rfloat_iter(m+6), rfloat_iter(m),
>> std::bind2nd(greater_equal<float>(), 14.5));
>
>Thanks for your help. First of all, what do you mean by 24.1.1?
>
Section 24.1.1 of the ISO C++ Standard (ISO/IEC 14882:2003).
>
>I am testing your code. To check if find_if finds something, should I
>use "p == rfloat_iter(m+6)" or "p == rfloat_iter(m)"?
The latter.
>
>Besides, is the range between m+5 and m-1?
>
Sort of. See 24.1.1 for more details.
>
>Your code has some compile errors. I appreciate if somebody can show
>me what's the correct syntax (because I am not familiar with this
>regard).

You need to #include <iterator>

I'm not surprised, I wrote it off the top of my head. I'm sure that
one of the other more knowledgeable types here can help you more than
me.
>
Also, as a matter of etiquette, please try not to top-post (posting all
your text above what you're replying to) -- it's frowned on in this
newsgroup. Instead, intersperse your replies with the text you're
referring to (as I did here), or after the text.

you might try this:

#include <iterator>
typedef std::reverse_iterator<float *rfloat_iter;
const rfloat_iter rbegin = rfloat_iter(m+6);
const rfloat_iter rend = rfloat_iter(m);
rfloat_iter p =
std::find_if(rbegin, rend,
std::bind2nd(std::greater_equal<float>(),
14.5));
if (p == r_end)
/* not found */;
Hi Red,

The compiler doesn't like the following line so the typedef statement
fails:
reverse_iterator<float *>

Thanks for your help.

Tony
I *think*, but am not positive, he meant something like:
std::vector<float*>::reverse_iterator

No, he did not: the whole point is to turn pointers (used as iterators into
a raw array) into reverse_iterators into that same array. There is no
vector in any of this.
To the OP:

The following (which is the code from red floyd) compiles on g++ and Comeau.

#include <iterator>
#include <algorithm>
#include <functional>

typedef std::reverse_iterator<float *rfloat_iter;

int main ( void ) {
float m[6];
const rfloat_iter rbegin = rfloat_iter(m+6);
const rfloat_iter rend = rfloat_iter(m);
rfloat_iter p =
std::find_if(rbegin, rend,
std::bind2nd(std::greater_equal<float>(),
14.5));
}

If you still can't get the code to compile, please post a short but complete
program that demonstrates the problem.
Best

Kai-Uwe Bux
Sorry, but isn't rend supposed to be initialised with rfloat_iter(m-1)?
Otherwise it will not test the element at index 0.
Adrian

--
__________________________________________________ ___________________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ My newsgroup writings are licensed under the Creative Commons /
\ Attribution-Noncommercial-Share Alike 3.0 License /
\_____[http://creativecommons.org/licenses/...sa/3.0/]_____/
\/______[blog:__http://adrians-musings.blogspot.com/]______\/
Mar 17 '07 #11

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

Similar topics

14
by: mikets | last post by:
Hi there, I found out that very often I use the following construct: for (it = myvec.begin(); it != myvec.end(); ++it) { if (it->name() == "myname") break; } Certainly, the container the...
1
by: John Black | last post by:
Hi, In using find_if() you need to name a comparision function which normally is a static function, but sometimes it is really inconvinient for this, let's take this example, class MyClass{...
3
by: marco_segurini | last post by:
Hi, when the following code is executed bool Test1(); bool Test2(); .... if (Test1() || Test2()) { ... } ....
4
by: Michael | last post by:
Hi, I'm having a few problems getting the following find_if to work class Entity { }; class factory {
5
by: dave_if | last post by:
I have a vector of TCard objects, I am sorting them based on the the box field. That part works fine (finally!). I would then like to random_shuffle all cards that are in a particular box, that is,...
18
by: ma740988 | last post by:
Trying to get more acclimated with the use of function objects. As part of my test, consider: # include <vector> # include <iostream> # include <algorithm> #include <stdexcept> #include...
3
by: devel | last post by:
Hello, Could someone tell me why the find_if statement applied to my multimap dictionnary is doesn't compile? Does this algorithm doesn't work on a multimap? I don't understand why the...
1
by: AlanJSmith | last post by:
can some clever guru help please. I am porting some C++ 6.0 projects to C++ VS2005 and find_if no longer exists in vector, i am not sure what to do. i am only at begginer level so please dont get...
3
by: Misiu | last post by:
Hello everybody, How to avoid using struct IsDataType for comparison for find_if algorithm but use something like that what is now commented out in the code below? Regards, Misiu ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.