How do I get char * from vector<char>?
I have 开发者_运维技巧declared a vector like this
vector<char> vbuffer;
and used it as a receive buffer like this..
recv = read(m_fd, &m_vbuffer[totalRecv], SIZE_OF_BUFFER);
It seems like working and now I want to get the raw char data for parsing..
If I have defined a function like this..
char* getData(){
//return char data from the vector
}
How would I fill inside of the function? Thanks in advance..
in c++0x, simply do m_vbuffer.data()
As long as you don't resize the vector in any way, you can use &vbuffer[0]
as a pointer to the array. There are many operations that will invalidate pointers to a vector though, make sure you don't call any of them while you have a pointer in use.
This will give you a pointer to the first element in the vector, so you can use it like an array:
&m_vbuffer[0]
精彩评论