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

L

Leo jay

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.
 
B

Barry

Leo said:
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);
 
W

William Xu

Leo jay said:
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.
 
S

Salt_Peter

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') );
}
 
L

Leo jay

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,969
Members
47,524
Latest member
ecomwebdesign

Latest Threads

Top