Convert std::string to const char* or char*
2014-03-07 18:37
633 查看
Q:
How can I convert an
a
a
A:
If you just want to pass a
a function that needs
If you want to get a writable copy, like
Edit: Notice that the above is not exception safe. If anything between the new call and the delete call throws, you will leak memory, as nothing
will call delete for you automatically. There are two immediate ways to solve this.
delete the memory for you upon going out of scope:
This is the standard way (does not require any external library). You use
which completely manages the memory for you.
From:http://stackoverflow.com/questions/347949/convert-stdstring-to-const-char-or-char
How can I convert an
std::stringto
a
char*or
a
const char*?
A:
If you just want to pass a
std::stringto
a function that needs
const char*you can use
std::string str; const char * c = str.c_str();
If you want to get a writable copy, like
char *, you can do that with this:
std::string str; char * writable = new char[str.size() + 1]; std::copy(str.begin(), str.end(), writable); writable[str.size()] = '\0'; // don't forget the terminating 0 // don't forget to free the string after finished using it delete[] writable;
Edit: Notice that the above is not exception safe. If anything between the new call and the delete call throws, you will leak memory, as nothing
will call delete for you automatically. There are two immediate ways to solve this.
boost::scoped_array
boost::scoped_arraywill
delete the memory for you upon going out of scope:
std::string str; boost::scoped_array<char> writable(new char[str.size() + 1]); std::copy(str.begin(), str.end(), writable.get()); writable[str.size()] = '\0'; // don't forget the terminating 0 // get the char* using writable.get() // memory is automatically freed if the smart pointer goes // out of scope
std::vector
This is the standard way (does not require any external library). You use std::vector,
which completely manages the memory for you.
std::string str; std::vector<char> writable(str.size() + 1); std::copy(str.begin(), str.end(), writable.begin()); // get the char* using &writable[0] or &*writable.begin()
From:http://stackoverflow.com/questions/347949/convert-stdstring-to-const-char-or-char
相关文章推荐
- convert a std::string to const char* or char*
- cannot convert parameter 1 from 'std::string' to 'const char *'
- amule: FTBFS ("cannot convert 'UpnpString* {aka s_UpnpString*}' to 'const char*'")
- Convert std::string to std::wstring or LPCWSTR
- C++编译出现binding ‘const string {aka const std::__cxx11::basic_string<char>}’ to reference of type ‘std
- string转Char*( error C2440: '=' : cannot convert from 'const char *' to 'char *')
- [C# Tip]How to: Convert System::String to wchar_t* or char*
- : error: undefined reference to `cv::VideoCapture::open(std::__cxx11::basic_string<char, std::char_t
- c++ int convert to std::string 转换成std::string
- cannot convert from 'const int' to 'const char *'
- 'LoadLibraryW' : cannot convert parameter 1 from 'const char *' to 'LPCWSTR' 解决方案
- 报错 error C2664: 'CreateWindowExA' : cannot convert parameter 2 from 'unsigned short [10]' to 'const char *'
- std::string to char*
- 在UNICODE编码格式下使用cvSaveImage出现cannot convert from 'CString' to 'const char *
- error: no matching function for call to 'std::basic_ifstream<char>::open(std::string&)
- std::string to char*
- cannot convert wchar_t to const char*
- error C2664: 'TextOutW' : cannot convert parameter 4 from const char [5]' to LPCTSTR
- cannot convert from 'WCHAR [260]' to 'std::basic_string<_Elem,_Traits,_Ax>'
- const char * 和 std::string.c_str()是个危险的东西!