C++ String operator[]()
C++ String operator[]()
此函数用于获取字符串中位置pos处的字符。
语法
请考虑在位置" pos"处的字符串" str"和字符" ch"。语法为:
char ch = str.operator[ ](pos);
参数
pos : 它定义了要检索的字符的位置。
返回值
此函数返回在位置pos处指定的字符。
示例1
让我们看一个简单的示例。
#include<iostream>
using namespace std;
int main()
{
string str="C programs";
for(int i=0;i<str.length();i++)
cout<<str.operator[](i);
return 0;
}
输出:
示例2
让我们看另一个简单的示例。
#include<iostream>
using namespace std;
int main()
{
string s="54321";
for(int i=0;i<s.length();i++)
cout<<s.operator[](i);
return 0;
}
输出: