writing directly to std::string internal buffers

前端 未结 9 1136
猫巷女王i
猫巷女王i 2020-11-29 07:22

I was looking for a way to stuff some data into a string across a DLL boundary. Because we use different compilers, all our dll interfaces are simple char*.

Is ther

9条回答
  •  春和景丽
    2020-11-29 07:37

    Considering Patrick's comment I would say, it's OK and convenient/efficient to directly write into a std::string. I would use &s.front() to get a char *, like in this mex example:

    #include "mex.h"
    #include 
    void mexFunction(
        int nlhs,
        mxArray *plhs[],
        int nrhs,
        const mxArray *prhs[]
    )
    {
        std::string ret;
        int len = (int)mxGetN(prhs[0]);
        ret.reserve(len+1);
        mxGetString(prhs[0],&ret.front(),len+1);
        mexPrintf(ret.c_str());
    }
    

提交回复
热议问题