目录

data

描述 (Description)

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

声明 (Declaration)

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

const char* data() const;

C++11

const char* data() const noexcept;

C++14

const char* data() const noexcept;

参数 (Parameters)

没有

返回值 (Return Value)

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

异常 (Exceptions)

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

例子 (Example)

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

#include <iostream>
#include <string>
#include <cstring>
int main () {
   int length;
   std::string str = "sairamkrishna mammahe";
   char* cstr = "sairamkrishna mammahe";
   if ( str.length() == std::strlen(cstr) ) {
      std::cout << "str and cstr have the same length.\n";
      if ( memcmp (cstr, str.data(), str.length() ) == 0 )
         std::cout << "str and cstr have the same content.\n";
   }
  return 0;
}

样本输出应该是这样的 -

str and cstr have the same length.
str and cstr have the same content.  
↑回到顶部↑
WIKI教程 @2018