• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Debuggin a Simple STL Algorithm :: STL

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1kuphryn  Members

Posted 24 October 2002 - 08:24 PM

Hi. I would like to debug a simple algorithm that does not work using an STL solution. The solution works via iteration. std::list theList; for (unsigned int i = 0; i < 10; ++i) theList.push_back(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. theList.erase(iTheList); // 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. ----- // Given iTheList points to theList.begin(). while (*iTheList <= 6) ++iTheList // Now decrement until end of theList. while (*iTheList != theList.end() { *iTheList = *iTheList - 1; ++iTheList; } ----- The iterative solution above works perfect. I would like to implement a more efficient and elegant solution. Here is one solution using STL algorithms. However, it does not work correctly. ----- std::for_each(std::find_if(theList.begin(), theList.end(), std::bind2nd(std::greater<>, 6)), theList.end(), std::bind2nd(std::minus<>, 1)); ----- The STL solution above does not work. The logic and syntec seem to be valid. Is there a logic or syntec misunderstanding in the STL solution? I looked over the function objects and function adapter. They are valid. Thanks, KUphryn

### #2Fruny  Moderators

Posted 24 October 2002 - 08:36 PM

for_each never assigns the result of the operation back, it just calls the function.
You are just doing value - 1, not value = value - 1.
That's the job of transform.

itor = theList.erase( std::find( theList.begin(), theList.end(), 6 ) );
std::transform( itor, theList.end(), itor, std::bind2nd( std::minus<int>(), 1 ) );

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

[edited by - Fruny on October 25, 2002 4:01:04 AM]

### #3kuphryn  Members

Posted 24 October 2002 - 08:55 PM

Nice!!! Thanks.

I will test your solution by tomorrow.

Kuphryn

### #4kuphryn  Members

Posted 25 October 2002 - 05:26 AM

Okay. I added a class object that will do the minus manually.

  template <typename T>class DecreaseOne : public std::binary_function<T, T, void>{   public:      void operator()(T &lp, const T &rp)      {         lp -= rp;      }};

Here the call to it.

  std::for_each(std::find_if(theList.begin(), theList.end(), std::bind2nd(std::greater<size_t>(), 6)), theList.end(), std::bind2nd(DecreaseOne<size_t>(), 1));

The compiler will not compile. Here is the errors.

  error C2064: term does not evaluate to a functionerror C2662: 'DecreaseOne<unsigned int>::operator()'' : cannot convert 'this' pointer from 'const DecreaseOne<T>' to 'DecreaseOne<T> &'        with        [            T=size_t        ]        and        [            T=size_t        ]  `

I defined DecreaseOne functor as a global class.

What is the problem?

Thanks,
Kuphryn

[edited by - kuphryn on October 25, 2002 12:27:44 PM]

### #5kuphryn  Members

Posted 25 October 2002 - 09:02 AM

Fruny: Okay. Your algorithm works perfect.

Thanks again,
Kuphryn

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.