Debuggin a Simple STL Algorithm :: STL
I would like to debug a simple algorithm that does not work using an STL solution. The solution works via iteration.
std::list<unsigned int> theList;
for (unsigned int i = 0; i < 10; ++i)
// theList should now contain 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.
// Remove "6" from the list.
// Assume that iTheList is an iterator that points to element with value 6.
// After erase() theList should now contain 0, 1, 2, 3, 4, 5, 7, 8, and 9.
Okay. Everything above works as designed. Now I would like to decrement everything bigger or equal to "6," thus make theList hold 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. Here is the iterative solution.
The iterative solution above works perfect.
// Given iTheList points to theList.begin().
while (*iTheList <= 6)
// Now decrement until end of theList.
while (*iTheList != theList.end()
*iTheList = *iTheList - 1;
I would like to implement a more efficient and elegant solution. Here is one solution using STL algorithms. However, it does not work correctly.
The STL solution above does not work. The logic and syntec seem to be valid.
std::for_each(std::find_if(theList.begin(), theList.end(), std::bind2nd(std::greater<>, 6)), theList.end(), std::bind2nd(std::minus<>, 1));
Is there a logic or syntec misunderstanding in the STL solution? I looked over the function objects and function adapter. They are valid.