[ create a new paste ] login | about

Link: http://codepad.org/0LM93Hhv    [ raw code | output | fork ]

C++, pasted on Jun 19:
#include <iostream>
#include <iterator>
#include <algorithm>

struct xyz {
    int a;
    int b;
};

bool findit(const xyz& a) {
    return (a.a == 2 && a.b == 3);
}

int main() {
    xyz begin[] = { {1, 2}, {2, 3}, {2, 3}, {3, 5} };
    xyz* end = begin + 4;
    
    // Forward find
    xyz* found = std::find_if(begin, end, findit);
    if (found != end)
        std::cout << "Found at position "
                  << found - begin
                  << std::endl;

    // Reverse find
    found = std::find_if(std::reverse_iterator<xyz*>(end),
                         std::reverse_iterator<xyz*>(begin),
                         findit);
    if (found != std::reverse_iterator<xyz*>(end));
        std::cout << "Found at position "
                  << found - std::reverse_iterator<xyz*>(end)
                  << std::endl;

    return 0;
}


Output:
1
2
3
In function 'int main()':
Line 28: error: cannot convert 'std::reverse_iterator<xyz*>' to 'xyz*' in assignment
compilation terminated due to -Wfatal-errors.


Create a new paste based on this one


Comments: