无涯教程-进程 - char& at(int pos)函数

64 阅读1分钟

此函数用于访问单个字符。

at - 语法

考虑一个字符串str。要找到特定字符的位置,其语法应为

str.at(pos);

at - 参数

pos  -  它定义字符在字符串中的位置。

at - 返回值

它返回在该位置指定的字符。

at - 例子1

#include<iostream>
using namespace std;
int main()
{
string str = "Welcome to learnfk tutorial";
cout<<"String contains :";
for(int i=0; i<str.length(); i++)
{
cout<<  str.at(i);
} 
cout<<
;
cout<<"String is shown again";
for(int j=0 ; j<str.length(); j++)
{
cout<< str[j];
}
return 0;
} 

输出:

String contains Welcome to learnfk tutorial

示例显示了我们可以使用成员函数at()或下标operator []访问字符。

参考链接

www.learnfk.com/c++/cpp-str…