beg2 == end2 for find_first_of and find_end.

  • Thread starter lovecreatesbea...
  • Start date
L

lovecreatesbea...

Can the 3rd and 4th arguments of find_first_of(beg1, end1, beg2, end2)
and find_end(beg1, end1, beg2, end2) be the same? I got an invalid
iterator on following 2 calls:

pos_ic_b = find_first_of(vecIcFile.begin(), vecIcFile.end(), it,
it);
pos_ic_e = find_end(vecIcFile.begin(), vecIcFile.end(), it, it);

But the following 2 calls are successful:

pos_ic_b = lower_bound(vecIcFile.begin(), vecIcFile.end(), *it);
pos_ic_e = upper_bound(vecIcFile.begin(), vecIcFile.end(), *it);

Can't these two pairs of functions do the same task?
 
D

Daniel T.

Can the 3rd and 4th arguments of find_first_of(beg1, end1, beg2, end2)
and find_end(beg1, end1, beg2, end2) be the same?

Yes, but in that case the function will always return a value equal to
end1, because the range of things you are searching for is empty.
I got an invalid iterator on following 2 calls:

pos_ic_b = find_first_of(vecIcFile.begin(), vecIcFile.end(), it,
it);
pos_ic_e = find_end(vecIcFile.begin(), vecIcFile.end(), it, it);

pos_ic_b should end up equaling veclcFile.end(). Same with pos_ic_e.
But the following 2 calls are successful:

pos_ic_b = lower_bound(vecIcFile.begin(), vecIcFile.end(), *it);
pos_ic_e = upper_bound(vecIcFile.begin(), vecIcFile.end(), *it);

lower_bound assumes a sorted range.
Can't these two pairs of functions do the same task?

find_first_of and lower_bound will "do the same task" only if the third
and fourth arguments define a range that contains only one element value.

For example:

void fn( vector<int>& vec, int i )
{
vector<T> sub( 1, i ); // could be (2, i) or (3, i) etc...
vector<int>::iterator it1 =
find_first_of( vec.begin(), vec.end(), sub.begin(), sub.end() );

vector<int>::iterator it2 = find( vec.begin(), vec.end(), i );

vector<int>::iterator it3 = lower_bound( vec.begin(), vec.end(), i );

assert( it1 == it2 );

if ( is_sorted( vec.begin(), vec.end() )
assert( it1 == it3 );
}
 
L

lovecreatesbea...

"Daniel T. wrote:"
Yes, but in that case the function will always return a value equal to
end1, because the range of things you are searching for is empty.

Thank you.

I write it as following to specify a single element as the second
range, is it right?

pos_ic_b = find_first_of(vecIcFile.begin(), vecIcFile.end(), it, it
+ 1);
pos_ic_b should end up equaling veclcFile.end(). Same with pos_ic_e.


lower_bound assumes a sorted range.

The elements of the vectors in my code are type of structure. They have
customized sort criteria. I overloaded operator < () for the structure
type.
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

"Daniel T. wrote:"


I write it as following to specify a single element as the second
range, is it right?

pos_ic_b = find_first_of(vecIcFile.begin(), vecIcFile.end(), it, it
+ 1);

Yes, that should work as long as 'it' is a random access iterator (it
won't compile otherwise).
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top