跳到主要内容

std::string front() 方法

// Non const version
constexpr CharT& front();

// Const version
constexpr const CharT& front() const;

返回对指定索引pos处元素的引用

指向字符串中的第一个字符。

未定义行为

在空字符串上调用 front()未定义行为

.

参数

(无)

返回值

指向第一个字符的引用。

复杂度

常数 - O(1)

备注

对于字符串 str,表达式 str.front() 等价于 *str.begin()str[0]

示例

Main.cpp
#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
本文档源自此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。

std::string front() 方法

// Non const version
constexpr CharT& front();

// Const version
constexpr const CharT& front() const;

返回对指定索引pos处元素的引用

指向字符串中的第一个字符。

未定义行为

在空字符串上调用 front()未定义行为

.

参数

(无)

返回值

指向第一个字符的引用。

复杂度

常数 - O(1)

备注

对于字符串 str,表达式 str.front() 等价于 *str.begin()str[0]

示例

Main.cpp
#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
本文档源自此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。