pointers - Write to a specific position in vector in C++ -


i have library function reads socket , writes content vector:

writeintovector( vector<char>& avector, int nr_of_bytes  ); 

i reading data in chunks socket , want place content read socket directly vector. such first writeintovector writes data vector position 0, next call writeintovector writes data socket vector @ position nr_of_bytes/2 (in case of char) , on. suspect can done using pointers not sure how it.

assuming writeintovector() own function, try this, using indices:

size_t writeintovector(const size_t startpos, std::vector<char> & out) {   // write startpos @ out.size() - 1   // return number of written elements } 

an iterator-based approach this:

typedef std::vector<int>::iterator iter;  iter writeintorange(iter begin, iter end) {   // write begin @ end - 1   // return last iterator, modified } 

Comments