How to copy a part of a vector in a raw memory

Hi,

How can I copy a part of a vector into a memory:

1
2
3
4
5
6
7
8
9
long ReadAt(T* pData, long items, long& pos)
{
  ... check vector bound ....

   for(uint i=0; i<items; i++, pos++) 
   {
     *pData++ = buffer[pos];
   }
}


Daniele.
I don't see any vector in there...
Sorry, I omitted some..

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

template<typename T>
class CBuffer
{
  private:
    std::vector<T>  buffer;

  public:
   long ReadAt(T* pData, long items, long& pos)
   {
    ... check vector bound ....

     for(uint i=0; i<items; i++, pos++) 
     {
       *pData++ = buffer[pos];
     }
   }    

};


Daniele.
Was memory at pData allocated?
Of course :-)

Like this code works, I would ask if there is a way to substitute the for() loop with, for example, the std::copy or with more efficient way.

Regards,
Daniele.
You can use the copy algorithm but it does almost the same thing. It's just shorter to call.
On the reference page you can see a sample implementation of std::copy. You can try to apply it on your code
http://www.cplusplus.com/reference/algorithm/copy/
Topic archived. No new replies allowed.