C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ String front()

C++ String front()

此函数用于引用字符串的第一个字符。

语法

考虑字符串str。语法为:
char& p = str.front();

参数

此函数不包含任何参数。

返回值

它用于返回第一个字符的引用。

示例1

让我们看一个简单的示例。
#include<iostream>
#include<string>
using namespace std;
int main()
{
    string str ="12345";
    cout<<str.front();
    return 0;
}
输出:
1

示例2

让我们看另一个简单的示例。
#include<iostream>
#include<string>
using namespace std;
int main()
{
    string str ="lidihuo";
    cout<<str.front();
    return 0;
}
输出:
j

示例3

让我们看一个使用front()函数修改第一个字符的简单示例。
#include<iostream>
#include<string>
using namespace std;
int main()
{
    string str ="hello World";
    str.front()='H'; 
    cout<<str;
    return 0;
}
输出:
Hello World

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4