Connecting Tech Pros Worldwide Forums | Help | Site Map

how to find the first iterator that not equals to some specifiedvalue?

Leo jay
Guest
 
Posts: n/a
#1: Jan 17 '08
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.

Barry
Guest
 
Posts: n/a
#2: Jan 17 '08

re: how to find the first iterator that not equals to some specifiedvalue?


Leo jay wrote:
Quote:
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<intconst& 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
William Xu
Guest
 
Posts: n/a
#3: Jan 17 '08

re: how to find the first iterator that not equals to some specifiedvalue?


Leo jay <Python.LeoJay@gmail.comwrites:
Quote:
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
Salt_Peter
Guest
 
Posts: n/a
#4: Jan 17 '08

re: how to find the first iterator that not equals to some specifiedvalue?


On Jan 16, 10:20 pm, Leo jay <Python.Leo...@gmail.comwrote:
Quote:
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') );
}
Leo jay
Guest
 
Posts: n/a
#5: Jan 17 '08

re: how to find the first iterator that not equals to some specifiedvalue?


On Jan 17, 12:46 pm, Salt_Peter <pj_h...@yahoo.comwrote:
Quote:
On Jan 16, 10:20 pm, Leo jay <Python.Leo...@gmail.comwrote:
>
Quote:
dear all,
>
Quote:
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?
>
Quote:
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.
Closed Thread