Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
461 views
in Technique[技术] by (71.8m points)

c++ - How can I get a pointer to the first element in an std::vector?

I want to write data from an std::vector<char> to a socket, using the write function, which has this prototype:

ssize_t write(int, const void *, size_t);

The second argument to this function must be a pointer to the first element in the std::vector<char>. How can I get a pointer to this element?

I tried std::vector::front but this returns a reference, while I need a pointer.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

C++11 has vec.data() which has the benefit that the call is valid even if the vector is empty.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...