std::basic_string<CharT,Traits,Allocator>:: front
| Classes | ||||
|
(C++17)
|
||||
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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++标准。
| DR | 适用范围 | 发布时的行为 | 正确行为 |
|---|---|---|---|
| LWG 534 | C++98 |
std::basic_string
没有成员函数
front()
|
已添加 |
参见
|
(
DR*
)
|
访问最后一个字符
(公开成员函数) |
|
访问第一个字符
(
std::basic_string_view<CharT,Traits>
的公开成员函数)
|