-
February 17th, 2012, 03:03 PM
#1
[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:eque 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:eque<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 03:38 PM.
Reason: Added compiler version
-
February 17th, 2012, 03:24 PM
#2
Re: Move elements from one container to another without loop
Originally Posted by Yadrif
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
-
February 17th, 2012, 03:31 PM
#3
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 03:35 PM.
-
February 17th, 2012, 03:42 PM
#4
Re: Move elements from one container to another without loop
Originally Posted by Yadrif
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
-
February 17th, 2012, 03:53 PM
#5
Re: Move elements from one container to another without loop
Originally Posted by Yadrif
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
-
February 17th, 2012, 04:20 PM
#6
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
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.
-
February 17th, 2012, 04:22 PM
#7
Re: Move elements from one container to another without loop
Originally Posted by Yadrif
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.
-
February 17th, 2012, 04:33 PM
#8
Re: Move elements from one container to another without loop
Originally Posted by Yadrif
Is your example only removing the elements from dataQ that are copied to data_array?
I see
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
-
February 17th, 2012, 04:35 PM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|