最近研究了一下基于c++的大数乘法算法, 碰到了string类对象与c风格字符串转换问题,有一个问题令我印象深刻,现在没有找到具体原因,记录下来。代码环境是Linux ubuntu,编译器是g++
觉得写得比较好的博客罗列如下:
【转】https://www.cnblogs.com/lifexy/p/8642163.html
1.c_str()
c_str()方法返回一个const char* 类型的指针变量, 该指针变量指向一个字符数组, 字符数组的元素个数是string::length() + 1,最后一个元素为 '\0'
以下为官方说法:
const charT* c_str() const;
Returns: A pointer to the initial element of an array of length size() + 1 whose first size() elements equal the corresponding elements of the string controlled by *this and whose last element is a null character specified by charT().
Requires: The program shall not alter any of the values stored in the array. Nor shall the program treat the returned value as a valid pointer value after any subsequent call to a non-const member function of the class basic_string that designates