C++字符串

一 begin()与end()
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string str("How are you");
	string::iterator it;
	for(it=str.begin();it!=str.end();it++)
	{
		cout << *it;
	}
	return 0;
}
二 rbegin()与rend()
返回一个逆向迭代器,指向最后一个字符
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string str("How are you");
	string::reverse_iterator rit;
	for(rit=str.rbegin();rit<str.rend();rit++)
	cout << *rit;
	return 0;
}
三 length()与size()
返回字符串的长度
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string str("How are you");
	cout << "The length of str is" << " " 
	<< str.length()<< " " << "cahractors.\n";
	return 0;
}
#include <iostream>
#include <string>
using namespace std;

int main()
{
	
	string str("How are you");
	cout << "The length of str is" << " " 
	<< str.size()<< " " << "cahractors.\n";
	return 0;
}
四 substr()
返回某个字符串
#include <iostream>
#include <string>
using namespace std;

int main()
{
	
	string str("How are you");
	string str1,str2;
	size_t it;
	
	str1=str.substr(4,3);
	it=str.find("are");
	str2=str.substr(it);
	cout << str1 << ' ' << str2 << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/zztingfeng/article/details/54411604