此函数用于引用字符串的第一个字符。
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