目录

c_str

描述 (Description)

它返回一个指向数组的指针,该数组包含表示字符串对象当前值的以空字符结尾的字符序列(即C字符串)。

声明 (Declaration)

以下是std :: string :: c_str的声明。

const char* c_str() const;

C++11

const char* c_str() const noexcept;

C++14

const char* c_str() const noexcept;

参数 (Parameters)

没有

返回值 (Return Value)

它返回一个指向数组的指针,该数组包含表示字符串对象当前值的以空字符结尾的字符序列(即C字符串)。

异常 (Exceptions)

如果抛出异常,则字符串中没有变化。

例子 (Example)

在下面的示例中为std :: string :: c_str。

#include <iostream>
#include <cstring>
#include <string>
int main () {
   std::string str ("Please divide this sentance into parts");
   char * cstr = new char [str.length()+1];
   std::strcpy (cstr, str.c_str());
   char * p = std::strtok (cstr," ");
   while (p!=0) {
      std::cout << p << '\n';
      p = std::strtok(NULL," ");
   }
   delete[] cstr;
   return 0;
}

样本输出应该是这样的 -

Please
divide
this
sentance
into
parts
↑回到顶部↑
WIKI教程 @2018