Connecting Tech Pros Worldwide Help | Site Map

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

 
LinkBack Thread Tools Search this Thread
  #1  
Old January 17th, 2008, 02:25 AM
Leo jay
Guest
 
Posts: n/a
Default 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.

  #2  
Old January 17th, 2008, 03:15 AM
Barry
Guest
 
Posts: n/a
Default 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
  #3  
Old January 17th, 2008, 03:35 AM
William Xu
Guest
 
Posts: n/a
Default Re: how to find the first iterator that not equals to some specified value?

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
  #4  
Old January 17th, 2008, 03:55 AM
Salt_Peter
Guest
 
Posts: n/a
Default 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') );
}
  #5  
Old January 17th, 2008, 04:25 AM
Leo jay
Guest
 
Posts: n/a
Default 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.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.