string容器06之string字符串的字符存取

2021-03-02 16:57:45 浏览数 (1)

string字符串的字符存取

代码语言:javascript复制
#include<iostream>
using namespace std;
//string字符串的字符存取
void test() 
{
	string s = "dhy";
	//[]读取字符
	//s.size(); 字符串s的大小
	//s.empty();判断字符串s是否为空
	for (int i = 0; i < s.size(); i  )
	{
		cout << s[i] << " ";
	}
	cout << endl;
	//at读取字符
	for (int i = 0; i < s.size(); i  )
	{
		cout << s.at(i) << " ";
	}
	cout << endl;
	//[]修改字符
	s[0] = 'x';
	for (int i = 0; i < s.size(); i  )
	{
		cout << s[i] << " ";
	}
	cout << endl;
	//at修改字符
	s.at(0) = 'c';
	for (int i = 0; i < s.size(); i  )
	{
		cout << s[i] << " ";
	}
	cout << endl;


}
int main()
{
	test();
	system("pause");
	return 0;
}

0 人点赞