std::basic_string::data
| const CharT* data() const; | (1) |  | 
|---|---|---|
| CharT* data(); | (2) | (since C++17) | 
返回指向用作字符存储的基础数组的指针。指针的范围[data(); data() + size())是有效的,其中的值对应于字符串中存储的值。
| The returned array is not required to be null-terminated. If empty() returns true, the pointer is a non-null pointer that should not be dereferenced. | (until C++11) | 
|---|---|
| The returned array is null-terminated, that is, data() and c_str() perform the same function. If empty() returns true, the pointer points to a single null character. | (since C++11) | 
从data()可因下列情况而失效:
- 将对字符串的非Const引用传递给任何标准库函数,或
- 调用字符串上的非Const成员函数,不包括operator[](),,,at(),,,front(),,,back(),,,begin(),,,end(),,,rbegin(),,,rend()...
1%29修改通过data有未定义的行为。
2%29修改存储在data()+size()有未定义的行为。
参数
%280%29
返回值
指向基础字符存储的指针。
| data()i == operator for every i in [0, size()). | (until C++11) | 
|---|---|
| data() + i == &operator for every i in 0, size(). | (since C++11) | 
复杂性
常量。
例外
| (none) | (until C++11) | 
|---|---|
| noexcept specification: noexcept | (since C++11) | 
例
二次
#include <algorithm>
#include <cassert>
#include <cstring>
#include <string>
 
int main()
{
  std::string const s("Emplary");
  assert(s.size() == std::strlen(s.data()));
  assert(std::equal(s.begin(), s.end(), s.data()));
  assert(std::equal(s.data(), s.data() + s.size(), s.begin()));
  assert(0 == *(s.data() + s.size()));
}二次
另见
| front (C++11) | accesses the first character (public member function) | 
|---|---|
| back (C++11) | accesses the last character (public member function) | 
| c_str | returns a non-modifiable standard C character array version of the string (public member function) | 
 © cppreference.com在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

