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::stringconst c("Exemplary");charconst& 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> 的公开成员函数) |