c++的string 容器基础操作

#define _CRT_SECURE_NO_WARNINGS

#include
#include

using namespace std;
void test01()
{
	string s1;
	string s2(10, 'a');
	string s3(s2);
	string s4("hello");
}
//基本赋值操作
void test02()
{
	string s1;
	s1 = "hello";
	cout << s1 << endl;
	string s2;
	//s2.assign(s1);
	s2.assign("hello w");
	cout << s2 << endl;
}
//存取字符操作
void test03()
{
	string s = "hello world";
	for (int i = 0; i < s.size(); i++)
	{
		cout << s[i] << " ";
	}
	cout << endl;
	for (int i = 0; i < s.size(); i++)
	{
		cout << s.at(i) << " ";
	}
	cout << endl;
	//区别:[]访问元素时,越界不抛异常,直接挂
	//at越界会抛异常
	try
	{
		cout << s.at(100) << endl;
		//cout << s[100] << endl;
	}
	catch (out_of_range& ex)
	{
		cout << ex.what() << endl;
	}
}
//拼接操作
void test04()
{
	string s1 = "aaa";
	s1 += "bbb";
	s1 += 'c';
	cout << s1 << endl;
	s1.append("dddd", 3);//加前三个
	cout <

你可能感兴趣的:(c++,开发语言,算法)