How to get the address of the std::vector buffer start most elegantly?

后端 未结 11 2190
-上瘾入骨i
-上瘾入骨i 2020-12-03 18:12

I want to use std::vector for dynamically allocating memory. The scenario is:

int neededLength = computeLength(); // some logic here

// this will allocate t         


        
11条回答
  •  眼角桃花
    2020-12-03 18:28

    Well, you can remove one set of parens:

    &buffer[0]
    

    but that is the common, idiomatic way of doing it. If it really offends you, I suppose you could use a template - something like:

    template  
    T * StartOf( std::vector  & v ) {
        return &v[0];
    }
    

提交回复
热议问题