By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,897 Members | 1,955 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,897 IT Pros & Developers. It's quick & easy.

how to reverse find_if?

P: n/a
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
Share this Question
Share on Google+
10 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.