std::basic_string<CharT,Traits,Allocator>::front
從 cppreference.com
< cpp | string | basic string
CharT& front(); | (1) | (C++20 起為 constexpr ) |
const CharT& front() const; | (2) | (C++20 起為 constexpr ) |
返回到字符串中首字符的引用。
如果 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++ 標準。
缺陷報告 | 應用於 | 出版時的行為 | 正確行為 |
---|---|---|---|
LWG 534 | C++98 | std::string 沒有成員函數 front() | 已添加 |
[編輯] 參閱
(DR*) | 訪問最後的字符 (公開成員函數) |
訪問首個字符 ( std::basic_string_view<CharT,Traits> 的公開成員函數) |