Algorithms c++

I need to realize the alogorithm search_end with the help this example:
1
2
3
4
5
6
7
8
9
10
11
12
13
template < class InputIterator, class OutputIterator, class Predicate >

void copy_if( InputIterator first,InputIterator last, OutputIterator result,
Predicate Functor)

{ for ( ; first != last; ++first)
if (Functor (*first))


{ *result = *first; ++result; }
return;

}

This algorithm copies elements satisfying the Functor predicate from
the specified interval of the inlet container to the outlet. This algorithm with the above functors can be useful for the following tasks:
1
2
3
4
5
6
7
8
int a(10] = { 1,2,3,4,5,6,7,8,9,0}:
int bC10] ={0}:

copy_if(a, a+10, b, Odd());

Odd f;

copy_if(a, a+1l0, b, f):


That i have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  template <class BinaryPredicate, class BidirectionalIterator1, class BidirectionalIterator2>

BidirectionalIterator1

search_end(BidirectionalIterator1 first1, BidirectionalIterator1 last1, BidirectionalIterator2 first2, BidirectionalIterator2 last2, BinaryPredicate pred, bidirectional_iterator_tag, bidirectional_iterator_tag){
   
    if (first2 == last2) return last1; 

    BidirectionalIterator1 l1 = last1;
    BidirectionalIterator2 l2 = last2;
    --l2;

    while (true){
       
        while (true){
            if (first1 == l1)  return last1;
            if (pred(*--l1, *l2)) break;
        }
       
        BidirectionalIterator1 m1 = l1;
        BidirectionalIterator2 m2 = l2;

        while (true){
            if (m2 == first2) return m1;
            if (m1 == first1) return last1;
            if (!pred(*--m1, *--m2)){ break; }  
        }
    }
}


But what do i need to do after?
I tried to do somethinng that:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int main() {
      
        int array[] = { 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 };
        int begin = 0, end = 11;        
        int* ret =0 ;
        int a[3] = { 1, 2, 3 };
        search_end(array, array + 12, a, a + 3, Odd());
        Odd f;
       
        if (*ret == array[12]) {
           std::cout << "Did not find any subsequence matching { 1, 2, 3 }" << std::endl;
        }
        else {
            std::cout << "The last matching subsequence is at: " << *ret << std::endl;
        }

        int b[] = { 5, 2, 3 };
        search_end(array, array + end, b, b + 2);
        if (*b == array[end]) {
            std::cout << "Did not find any subsequence matching { 3, 2, 3 }" << std::endl;
        }
        else {
            std::cout << "The last matching subsequence is at: " << *b << std::endl;
        }    
}
Last edited on
> I need to realize the alogorithm search_end
¿what does it do?

1
2
3
4
        search_end(array, array + 12, a, a + 3, Odd()); //the returned value is discarded
        Odd f; //never used
       
        if (*ret == array[12]) { //out of bounds access 
Topic archived. No new replies allowed.