std::basic_string<CharT,Traits,Allocator>::front
From cppreference.com
< cpp | string | basic string
| CharT& front(); |
(1) | (C++20 以降 constexpr) |
| const CharT& front() const; |
(2) | (C++20 以降 constexpr) |
文字列の最初の文字への参照を返します。empty() が true の場合、動作は未定義です。
目次 |
[編集] パラメータ
(なし)
[編集] 戻り値
最初の文字への参照。operator[](0) と等価。
[編集] 計算量
定数。
[編集] 備考
libstdc++では、C++98モードでfront()は利用できません。
[編集] 例
このコードを実行
#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*) |
最後の文字にアクセスする (public member function) |
| 最初の文字にアクセスする ( std::basic_string_view<CharT,Traits>のpublicメンバ関数) |