首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

c_str函数是什么意思?该怎么解决

2012-03-02 
c_str函数是什么意思?C/C++ code#include iostream#include stringusing namespace stdint main(){st

c_str函数是什么意思?

C/C++ code
#include <iostream>#include <string>using namespace std;int main(){    string str;    cout<<"网站:";    cin>>str;    str="start "+str;    system(str.c_str());  //在这里这个c_str()函数是什么意思? }


[解决办法]
string类的一的成员函数,返一个C-style的字符串指针(char*),指向string中保存的字符串
[解决办法]
c_str()函数返回一个指向正规C字符串的指针, 内容与string串相同.
[解决办法]
Converts the contents of a string as a C-style, null-terminated string.

 
const value_type *c_str( ) const;
 

Return Value
A pointer to the C-style version of the invoking string. The pointer value is not valid after calling a non-const function, including the destructor, in the basic_string class on the object.

[解决办法]
标准C++库字符串类的一个成员函数,因为system函数只接受const char*类型的参数,而不是const std::string&,你就需要把后者转换为前者。c_str()就是做这个用的。所谓C风格的字符串,就是以\0作为结束符的字符串表达方法。

热点排行