把string转换为char*

来源:互联网 发布:手机上怎样申请淘宝号 编辑:程序博客网 时间:2024/05/16 11:58



https://stackoverflow.com/questions/347949/how-to-convert-a-stdstring-to-const-char-or-char




876down voteaccepted

If you just want to pass a std::string to 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 itdelete[] writable;

Edit: Notice that the above is not exception safe. If anything between the new call and the deletecall 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_array will 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.begin(), str.end());writable.push_back('\0');// get the char* using &writable[0] or &*writable.begin()
876down voteaccepted

If you just want to pass a std::string to 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 itdelete[] writable;

Edit: Notice that the above is not exception safe. If anything between the new call and the deletecall 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_array will 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.begin(), str.end());writable.push_back('\0');// get the char* using &writable[0] or &*writable.begin()
阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 1296次列车时刻表 1270列车时刻表 诸暨火车时刻表 霸州到聊城火车时刻表 阆中到南充火车时刻表 咸阳到彬县火车时刻表 k822列车时刻表 火车时候表 1158列车时刻表 天津到廊坊火车时刻表 揭阳火车时刻表 12606 车票预定 石家庄到邯郸的火车时刻表 z253火车时刻表 遂宁火车时刻表 车票查询 郑州到邯郸火车时刻表 大连高铁时刻表查询 阆中到重庆火车时刻表 廊坊北站列车时刻表 重庆到阆中火车时刻表 漯河至郑州火车时刻表 z230次列车时刻表 武广高铁时刻表查询 k1281列车时刻表 成都到苍溪火车时刻表 重庆到南部火车时刻表 火车时间表查询 重庆北站火车时刻表 洛阳高铁时刻表查询 高铁查询 网上购票 长沙至广州高铁时刻表查询 西安火车时刻表查询 蒙自到玉溪火车时刻表 成都到阆中火车时刻表 火车栗 蒙自至河口火车时刻表 高铁时刻表查询 车票