std::basic_string<CharT,Traits,Allocator>::front_C++中文网

C++ 参考手册

位置:首页 > C++ 参考手册 >字符串库 >std::basic_string > std::basic_string<CharT,Traits,Allocator>::front

返回到 string 中首字符的引用。若 empty() == true 则行为未定义。

参数

(无)

返回值

到首字符的引用,等价于 operator[](0) 。

复杂度

常数

示例

#include <iostream>
#include <string>
 
int main()
{
  {
    std::string s("Exemplary");
    char& f = s.front();
    f = 'e';
    std::cout << s << '\n'; // "exemplary"
  }
 
  {
    std::string const c("Exemplary");
    char const& f = c.front();
    std::cout << &f << '\n'; // "Exemplary"
  }
}

输出:

exemplary
Exemplary

参阅