此函数用于引用字符串的第一个字符。
char& p = str.front();
它用于返回第一个字符的引用。
让我们看一个简单的例子。
#include<iostream> #include<string> using namespace std; int main() { string str ="12345"; cout<<str.front(); return 0; }
输出:
1
让我们来看另一个简单的例子。
#include<iostream> #include<string> using namespace std; int main() { string str ="learnfk"; cout<<str.front(); return 0; }
输出:
j
让我们看一个简单的示例,使用front()函数修改第一个字符。
#include<iostream> #include<string> using namespace std; int main() { string str ="hello World"; str.front()='H'; cout<<str; return 0; }
输出:
Hello World
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)