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