Access string in content

1. Access by the subscript

(1) output of the entire string

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str;
	cin>>str;
	cout<<str;
	return 0;
}

(2) specify the output content

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str;
	cin>>str;
	for(int i=0;i<str.length();i++)//通过for循环指定输出内容
		cout<<str[i];
	return 0;
}

2. Access through the iterator

General by 1 to meet access requirements, but some functions such as erase () is required to iterators as parameters, but also to learn about the usage string iterators
Note: Unlike other containers, string parameter can be defined directly without the need for

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str="abcd";
	string::iterator it;//不同于其他容器,string可直接定义而不需要参数
	for(it=str.begin();it!=str.end();it++)
		cout<<*it;
	return 0;
}
Published 94 original articles · won praise 193 · Views 5591

Guess you like

Origin blog.csdn.net/weixin_45884316/article/details/104147389