std::basic_string<CharT,Traits,Allocator>:: front
| Classes | ||||
|
(C++17)
|
||||
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
CharT
&
front
(
)
;
|
(1) | (constexpr since C++20) |
|
const
CharT
&
front
(
)
const
;
|
(2) | (constexpr since C++20) |
文字列の最初の文字への参照を返します。
|
empty() が true の場合、動作は未定義です。 |
(C++26まで) |
|
empty() が true の場合:
|
(C++26以降) |
目次 |
戻り値
operator [ ] ( 0 )
計算量
定数。
注記
libstdc++では、
front()
はC++98モードでは
利用できません
。
例
#include <iostream> #include <string> int main() { std::string s("Exemplary"); char& f1 = s.front(); f1 = 'e'; std::cout << s << '\n'; // "exemplary" std::string const c("Exemplary"); char const& f2 = c.front(); std::cout << &f2 << '\n'; // "Exemplary" }
出力:
exemplary Exemplary
不具合報告
以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 正しい動作 |
|---|---|---|---|
| LWG 534 | C++98 |
std::basic_string
にはメンバー関数
front()
が存在しなかった
|
追加された |
関連項目
|
(
DR*
)
|
最後の文字にアクセスする
(公開メンバ関数) |
|
最初の文字にアクセスする
(
std::basic_string_view<CharT,Traits>
の公開メンバ関数)
|