How to put stringstream contents into char instead string type?

后端 未结 4 658
滥情空心
滥情空心 2021-01-17 21:10

Every one know stringstream.str() need a string variable type to store the content of stringstream.str() into it .

I want to store the cont

4条回答
  •  别那么骄傲
    2021-01-17 21:35

    You can do this if you want an actual copy of the string (vital if the stringstream object is going to go out of scope at some point):

    const char *p = new char[ss.str().size()+1];
    strcpy(p, ss.str().c_str());
    
    ...
    
    delete [] p;
    

    As discussed in comments below, you should be wary of doing it like this (manual memory management is error-prone, and very non-idiomatic C++). Why do you want a raw char array?

提交回复
热议问题