Debuggin a Simple STL Algorithm :: STL

This is a discussion on Debuggin a Simple STL Algorithm :: STL within the C++ Programming forums, part of the General Programming Boards category; Hi. I would like to debug a simple algorithm that does not work using an STL solution. The solution works ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348

    Debuggin a Simple STL Algorithm :: STL

    Hi.

    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.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.

    Code:
    // 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.

    Code:
    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

  2. #2
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    std::minus doesn't change the object, it just calculates the difference.

    And instead of
    Code:
    std::bind2nd(std::greater<>, 6))
    I had to write
    Code:
    std::bind2nd(std::greater<unsigned int>(), 6))
    to make it compile (CodeWarrior)
    Last edited by Sang-drax; 10-25-2002 at 06:52 AM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    You could write your own function object instead of minus:
    Code:
    template<class Type>
       struct decrease : public binary_function <Type, Type, Type> 
       {
          Type operator()(
             Type& _Left, 
             const Type& _Right
          ) const;
          {
              _Left -= _Right;
          }
       };
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Okay. Thanks.

    Fruny posted a solution at GameDev using transform().

    http://www.gamedev.net/community/for...opic_id=120974

    In general, what is the problem of my original STL solution?

    Kuphryn

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Okay. I added a class object that will do the minus manually.

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

    Here the call to it.

    Code:
    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.

    Code:
    error C2064: term does not evaluate to a function
    error 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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-24-2004, 12:22 AM
  2. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 08:26 PM
  3. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 04:49 AM
  4. sort(begin,end,op) stl algorithm
    By kes103 in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2003, 11:58 AM
  5. STL algorithm question
    By Reggie in forum C++ Programming
    Replies: 1
    Last Post: 04-22-2003, 10:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21