进程 中的 char& front()函数

首页 / C++入门教程 / 进程 中的 char& front()函数

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

front - 语法

char& p = str.front();

front - 返回值

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

front - 例子1

让我们看一个简单的例子。

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str ="12345";
	cout<<str.front();
	return 0;
}

输出:

1

front - 例子2

让我们来看另一个简单的例子。

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str ="learnfk";
	cout<<str.front();
	return 0;
}

输出:

j

front - 例子3

让我们看一个简单的示例,使用front()函数修改第一个字符。

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str ="hello World";
	str.front()='H';	
	cout<<str;
	return 0;
}

输出:

Hello World

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

代码精进之路 -〔范学雷〕

TensorFlow快速入门与实战 -〔彭靖田〕

分布式系统案例课 -〔杨波〕

乔新亮的CTO成长复盘 -〔乔新亮〕

手把手教你玩音乐 -〔邓柯〕

讲好故事 -〔涵柏〕

遗留系统现代化实战 -〔姚琪琳〕

LangChain 实战课 -〔黄佳〕

给程序员的写作课 -〔高磊〕

好记忆不如烂笔头。留下您的足迹吧 :)