名前空間
変種
操作

std::basic_string<CharT,Traits,Allocator>::c_str

提供: cppreference.com
< cpp‎ | string‎ | basic string
 
 
 
std::basic_string
 
const CharT* c_str() const;
(C++11未満)
const CharT* c_str() const noexcept;
(C++11以上)
(C++20未満)
constexpr const CharT* c_str() const noexcept;
(C++20以上)

文字列に格納されているのと同じデータを持つヌル終端文字配列を指すポインタを返します。

ポインタは範囲 [c_str(); c_str() + size()] が有効であり、その値は、最後の位置の後にある追加のヌル文字も含めて、文字列に格納されている値に対応しています。

c_str() から取得したポインタは以下の場合に無効化されるかもしれません。

c_str() を通してアクセスできる文字配列への書き込みは未定義動作です。

c_str()data() は同じ��能を持ちます。

(C++11以上)

目次

[編集] 引数

(なし)

[編集] 戻り値

ベースとなる文字記憶域を指すポインタ。

[0, size()) 内のすべての i について c_str()[i] == operator[](i) が成り立ちます。

(C++11未満)

[0, size()] 内のすべての i について c_str() + i == std::addressof(operator[](i)) が成り立ちます。

(C++11以上)

[編集] 計算量

一定。

[編集] ノート

c_str() から取得したポインタは、その文字列オブジェクトが他のヌル文字を含んでいない場合にのみ、ヌル終端文字列を指すポインタとして扱うことができます。

[編集]

#include <algorithm>
#include <cassert>
#include <cstring>
#include <string>
 
int main()
{
  std::string const s("Emplary");
  assert(s.size() == std::strlen(s.c_str()));
  assert(std::equal(s.begin(), s.end(), s.c_str()));
  assert(std::equal(s.c_str(), s.c_str() + s.size(), s.begin()));
  assert(0 == *(s.c_str() + s.size()));
}


[編集] 関連項目

(C++11)
最初の文字にアクセスします
(パブリックメンバ関数) [edit]
(C++11)
最後の文字にアクセスします
(パブリックメンバ関数) [edit]
文字列の最初の文字を指すポインタを返します
(パブリックメンバ関数) [edit]