[RESOLVED] Move elements from one container to another without loop
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: [RESOLVED] Move elements from one container to another without loop

  1. #1
    Join Date
    Oct 2007
    Posts
    31

    [RESOLVED] Move elements from one container to another without loop

    Is there an algorithm in the STL to move elements similar to how std::copy works? I have read various places that the new C++ standard has a move algorithm. Unfortunately the compiler I use (g++ (GCC) 4.2.0) does not support any C++0x updates.

    I have a std::deque that I want to move a range from into an array. I am currently using something like this where

    data_array is an unsigned char pointer to a buffer being passed in by the caller
    dataQ is a std::deque<unsigned char> that is a member variable maintained within the class

    Code:
        for (int i = 0; i < numberBytesRequested; ++i)
        {
        	data_array[i] = dataQ.front();
        	dataQ.pop_front();
        }
    I'm concerned that executing this loop over and over again is going to be very inefficient. I am looking for a more efficient way to do this.

    Thanks for the help.
    Last edited by Yadrif; February 17th, 2012 at 02:38 PM. Reason: Added compiler version

  2. #2
    Join Date
    Apr 1999
    Posts
    27,418

    Re: Move elements from one container to another without loop

    Quote Originally Posted by Yadrif View Post
    Is there an algorithm in the STL to move elements similar to how std::copy works?
    Isn't std::copy what you want to use?
    Code:
       //....
        unsigned char data_array[3000];
        const int numChars = sizeof(data_array) / sizeof(data_array[0]);
        
        std::deque<unsigned char> dataQ;
       //..
        std::copy(dataQ.begin(), dataQ.begin() + min( dataQ.size(), numChars ), data_array);
        dataQ.clear();
    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Oct 2007
    Posts
    31

    Re: Move elements from one container to another without loop

    I'm not taking all of the data from the dataQ only what is requested by the caller of the function. The size variable is the amount of data requested by the caller. Int he real code data_array is a pointer to an unsigned char buffer.

    I did not add all of the code from the function but it basically implements a cache that is filled when dataQ reaches a low mark. It is filled by requesting a predefined amount from another buffer that is being continually filled from some other process.

    Hope this clarifies what I am attempting to do.

    Thanks.
    Last edited by Yadrif; February 17th, 2012 at 02:35 PM.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,418

    Re: Move elements from one container to another without loop

    Quote Originally Posted by Yadrif View Post
    I'm not taking all of the data from the dataQ
    And neither am I. Look at my code carefully.

    You control how much you want to copy by giving the starting and ending iterators of the range. Take a look at the parameters to std::copy.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Apr 1999
    Posts
    27,418

    Re: Move elements from one container to another without loop

    Quote Originally Posted by Yadrif View Post
    The size variable is the amount of data requested by the caller. Int he real code data_array is a pointer to an unsigned char buffer.
    That doesn't really make any difference.
    Code:
    std::deque<unsigned char> mydeque;
    //...
    void SomeFunction(unsigned char *buffer, size_t numElements)
    {
        std::copy(mydeque.begin(), mydeque.begin() + 
                       min(mydeque.size(), numElements), buffer);
    }
    The reason for the min() is to make sure you don't go outside the deque's boundaries.
    cache that is filled when dataQ reaches a low mark. It is filled by requesting a predefined amount from another buffer that is being continually filled from some other process.
    Hopefully you used proper synchronization, as STL containers are not guaranteed to be thread safe.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Oct 2007
    Posts
    31

    Re: Move elements from one container to another without loop

    Is your example only removing the elements from dataQ that are copied to data_array?

    I see

    Code:
    dataQ.clear()
    But that will drop all elements not just what was copied with std::copy. Or maybe I am missing something from your example.

    An example of my situation is:
    dataQ will contain 2000 bytes and the caller will request 2 bytes which get copied to data_array then removed from dataQ. The other 1998 bytes should remain in dataQ for the next time the function is called. Also, there is another thread pushing bytes on to the end of dataQ.

    Thanks for your help.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,885

    Re: Move elements from one container to another without loop

    Quote Originally Posted by Yadrif View Post
    But that will drop all elements not just what was copied with std::copy.
    Correct; to remove just those elements, use the version of erase() taking two iterators.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,418

    Re: Move elements from one container to another without loop

    Quote Originally Posted by Yadrif View Post
    Is your example only removing the elements from dataQ that are copied to data_array?

    I see

    Code:
    dataQ.clear()
    But that will drop all elements not just what was copied with std::copy. Or maybe I am missing something from your example.
    http://www.cplusplus.com/reference/stl/deque/

    deque::erase().
    dataQ will contain 2000 bytes and the caller will request 2 bytes which get copied to data_array then removed from dataQ. The other 1998 bytes should remain in dataQ for the next time the function is called. Also, there is another thread pushing bytes on to the end of dataQ.
    Again, hopefully you're using synchronization objects, as STL is not guaranteed to be thread-safe.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Oct 2007
    Posts
    31

    Re: [RESOLVED] Move elements from one container to another without loop

    Thanks for everyone's help. I was so focused on finding an algorithm to do this in one call I didn't think to copy then erase.

    Once again thank you guys for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center