C + + converts std::string to char*

Source: Internet
Author: User
Reference:

std::string to char*

C + + converts std::string to char*

There is currently no method for direct conversion. The C-style string must be obtained through the method of the string object c_str() :

Note that this method returns a type const char * that cannot directly modify the returned C-style string and, if necessary, must first copy the string:

std::string str = "string";char *cstr = new char[str.length() + 1];strcpy(cstr, str.c_str());// 对cstr的处理delete [] cstr;

Another way to do this is to store the string converted by string C-style in a vector :

std::vector<char> cstr(str.c_str(), str.c_str()+str.size()+1);

2018.7.2

C + + converts std::string to char*

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.