KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
To search the first occurence of a element in a C-Array with POD elements, one ease can do this with std::find_if(begin, end, findit) . But I needed the last occurence. This answer gave me the idea that this can be done with std::reverse_iterator . Thus I tried: std::find_if(std::reverse_iterator<podtype*>(end), std::reverse_iterator<podtype*>(begin), findit); This gave me the error: cannot convert 'std::reverse_iterator< xyz* > ' to 'xyz*' in assignment Do you have an idea how to do it this way or do you know a better solution? This is the code: #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; } And the compiler error on codepad.org
Tags (comma-separated)
Save Edits
Cancel