Algorithm search_end_if

I wrote an algorithm using a functor. For example, there is a condition that the elements must be greater than zero.
But this implementation doesn't work. Currently displayed Not found. And should be Found on 1. Why is that? Tried different conditions like i% 2 = 1 (with different sequence). So, the result is correct (Found on 1), but in fact, set
i% 2 = 0.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
template<class ForwardIterator1, class ForwardIterator2, class Predicate>
ForwardIterator1 search_end_if(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, Predicate Functor){
 
    ForwardIterator1 ret = last1;
 
    for (; first1 != last1; ++first1) {
        ForwardIterator1 it1 = first1;
        ForwardIterator2 it2 = first2;
 
        while (*it1 == *it2) {
            ++it1;
            ++it2;
            if (Functor(*last2)) {
                ret = first1;
                break;
            }
            if (Functor(*last1)) return ret;
        }
    }
    return ret;
}
 
class Odd {
public:
    bool operator()(int i) {
        return i > 0;
    }
};
 
 
int main() {
    int* n;
    int x[3] = { 41, 1, 2 };
    int y[2] = { 1, 2 };
 
    search_end_if(x, x + 3, y, y + 2, Odd());
    Odd f;
    n = search_end_if(x, x + 3, y, y + 2, f);
   
    if (n == x + 3) std::cout << "\nNot found" << std::endl;
    else std::cout << "\nFound on position number " << (n - x) << std::endl;
 
    return 0;
}
You're calling your function on last2/last1. You shouldn't be dereferencing a "last" iterator... x + 3, for instance, does not point to a valid element.

I don't think you've really explained what your algorithm is supposed to do.
- Why does it take in two different sequences? It sounds like you're saying the algorithm should "search until it finds the matching condition".
- Why is your class called Odd if the condition is i > 0? Why do you even need a class (there's no state; it could just be a function/non-capturing lambda)?

Maybe show a few more input/output examples so that we can get the idea of what the algorithm is supposed to do.
Last edited on

Ganado


thank you
I need to make search_end and search_end_if. I realized search_end and it works:

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
31
32
33
34
35
36
37
#include<iostream>
#include<algorithm>

template<class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 search_end(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2){
    if (first2 == last2) return last1;  

    ForwardIterator1 ret = last1;

    for (; first1 != last1;  ++first1) {
        ForwardIterator1 it1 = first1;
        ForwardIterator2 it2 = first2;
        while (*it1 == *it2) {    
            ++it1;
            ++it2;
            if (it2 == last2) {
                ret = first1;
                break;
            }
            if (it1 == last1) return ret;
        }       
    }
    return ret;
}

int main() {

    int* c;
    int a[10] = { 1, 26, 33, 40, 0, 1, 26, 83, 41, 50 };
    int b[3] = { 1, 26 };
  
    c =  search_end(a, a + 10, b, b + 2);

    if (c == a + 10) std::cout << "Not found" << std::endl;
    else std::cout << "Found on position number " << (c - a) << std::endl;
    return 0;
}


As I understend to realized search_end_if I need to take search_ end and to add Functor. So, we will have some condition. For example, i > 0.
while (Functor(*it1, *it2))
while (Functor(*it1, *it2))


Thanks! Can I make this code in files like .h, .cpp?
Last edited on
Thanks! Can I make this code in files like .h, .cpp?

Um... where else would you put it?
It can go into a header included file (like the standard library), but can't into a separate compilation unit (templated code must be part of the same compilation unit).
Um... where else would you put it?
I started learning it not so far. So I asked it)
It can go into a header included file (like the standard library), but can't into a separate compilation unit (templated code must be part of the same compilation unit).

Thank you!
Topic archived. No new replies allowed.