DEVELOP > c-Plus-Plus > how to find the first iterator that not equals to some specifiedvalue?
| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Leo jay" |
| Date: |
16 Jan 2008 09:20:31 PM |
| Object: |
how to find the first iterator that not equals to some specifiedvalue? |
dear all,
i know i can use std::find to find specified value in an iterator
range,
i'd like to know is there any way to find the first value that not
equals to the specified value?
just like string::find_first_not_of,
or do i have to write a for loop and check manually?
thanks in advance.
.
|
|
| User: "William Xu" |
|
| Title: Re: how to find the first iterator that not equals to some specified value? |
16 Jan 2008 10:32:19 PM |
|
|
Leo jay <Python.LeoJay@gmail.com> writes:
i know i can use std::find to find specified value in an iterator
range,
i'd like to know is there any way to find the first value that not
equals to the specified value?
just like string::find_first_not_of,
or do i have to write a for loop and check manually?
Try std::find_if.
--
William
http://williamxu.net9.org
.
|
|
|
|
| User: "Barry" |
|
| Title: Re: how to find the first iterator that not equals to some specifiedvalue? |
16 Jan 2008 10:13:57 PM |
|
|
Leo jay wrote:
dear all,
i know i can use std::find to find specified value in an iterator
range,
i'd like to know is there any way to find the first value that not
equals to the specified value?
just like string::find_first_not_of,
or do i have to write a for loop and check manually?
thanks in advance.
find_if: the predicate version of find
bool not_equal_to_1(int i) {
return i != 1;
}
void foo(std::vector<int> const& V) {
std::vector<int>::const_iterator pos =
std::find_if(V.begin(), V.end(), not_equal_to_1);
}
if you borrowing Boost.Lambda, it could be much simpler.
std::find_if(V.begin(), V.end(), _1 != 1);
--
Thanks
Barry
.
|
|
|
|
| User: "Salt_Peter" |
|
| Title: Re: how to find the first iterator that not equals to some specifiedvalue? |
16 Jan 2008 10:46:18 PM |
|
|
On Jan 16, 10:20 pm, Leo jay <Python.Leo...@gmail.com> wrote:
dear all,
i know i can use std::find to find specified value in an iterator
range,
i'd like to know is there any way to find the first value that not
equals to the specified value?
just like string::find_first_not_of,
or do i have to write a for loop and check manually?
thanks in advance.
std::find_if and std::not_equal_to should do nicely.
assuming a vector of char:
#include <vector>
#include <algorithm> // find_if
#include <functional> // bind1st, not_equal_to
int main()
{
...
std::vector<char>::const_iterator it
= std::find_if( vc.begin(),
vc.end(),
std::bind1st (std::not_equal_to<char>(),
'a') );
}
.
|
|
|
| User: "Leo jay" |
|
| Title: Re: how to find the first iterator that not equals to some specifiedvalue? |
16 Jan 2008 11:18:33 PM |
|
|
On Jan 17, 12:46 pm, Salt_Peter <pj_h...@yahoo.com> wrote:
On Jan 16, 10:20 pm, Leo jay <Python.Leo...@gmail.com> wrote:
dear all,
i know i can use std::find to find specified value in an iterator
range,
i'd like to know is there any way to find the first value that not
equals to the specified value?
just like string::find_first_not_of,
or do i have to write a for loop and check manually?
thanks in advance.
std::find_if and std::not_equal_to should do nicely.
assuming a vector of char:
#include <vector>
#include <algorithm> // find_if
#include <functional> // bind1st, not_equal_to
int main()
{
...
std::vector<char>::const_iterator it
= std::find_if( vc.begin(),
vc.end(),
std::bind1st (std::not_equal_to<char>(),
'a') );
}
thanks, that's what i'm looking for.
.
|
|
|
|
|

|
Related Articles |
|
|