STL--string容器(学习)

string基本概念

string和char*区别:

  • char*是一个指针
  • string是一个类,内部封装了char*,是一个char*的容器

特点:

string内部封装了很多成员方法:查找find,拷贝copy,delete删除,替换replace等等

string管理char*所分配的内存,不用担心赋值越界和取值越界等,有类的内部负责

string构造函数

#include<iostream>
#include<vector>
#include<string>
using namespace std;


void test01()
{
	string s1;

	const char* str = "hello world";


	string s2(str);
	cout << s2 << endl;

	string s3(s2);
	cout << s3 << endl;

	string s4(10, 'a');
	cout << s4 << endl;
}

int main()
{
	test01();

	system("pause");
	return 0;
}

string赋值操作

#include<iostream>
#include<vector>
#include<string>
using namespace std;


void test01()
{
	string s1;
	s1 = "hello world";
	cout << s1 << endl;


	string s2;
	s2 = s1;
	cout << s2 << endl;


	string s3;
	s3 = 'a';
	cout << s3 << endl;


	string s4;
	s4.assign("hello,C++");
	cout << s4 << endl;


	string s5;
	s5.assign("Hello,C++", 5);
	cout << s5 << endl;


	string s7;
	s7.assign(10, 'w');
	cout << s7<<endl;
}
int main()
{
	test01();

	system("pause");
	return 0;
}

string字符串拼接

#include<iostream>
#include<vector>
#include<string>
using namespace std;


void test01()
{
	string s1;
	s1 = "我";
	s1 += "爱玩游戏";
	cout << s1 << endl;

	s1 += '!';
	cout << s1 << endl;

	string s2;
	s2 = "嘿嘿";
	s1 += s2;
	cout << s1 << endl;


	string s3;
	s3 = 'I';
	s3.append("love");
	cout << s3 << endl;


	s3.append("game.ss", 6);
	cout << s3 << endl;

	s3.append(s2);
	cout << s3 << endl;

	s3.append(s1, 0, 4);
	cout << s3 << endl;


}
int main()
{
	test01();

	system("pause");
	return 0;
}

 string查找和替换

#include<iostream>
#include<vector>
#include<string>
using namespace std;


void test01()
{
	string s1="abcdefgde";

	int ret = s1.find("de");

	if (ret == -1)
	{
		cout << "没找到" << endl;
	}
	else
	{
		cout << ret << endl;
	}

	//rfind    rfind从右往左查找   find从左往右查
	ret = s1.rfind("de");
	cout << ret << endl;
}


void test02()
{
	string s1 = "sdasdasdasfwegersd";

	s1.replace(1, 3, "ytiyui");
	cout << s1;

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

string字符串比较

#include<iostream>
#include<vector>
#include<string>
using namespace std;


void test01()
{
	string s1 = "hello world1";
	string s2 = "hello world";


	if (s1.compare(s2) == 0)
	{
		cout << "相等" << endl;
	}
	else if(s1.compare(s2) == 1)
	{
		cout << "da" << endl;
	}
	else
	{
		cout << "xiao" << endl;
	}
}

int main()
{
	test01();

	system("pause");
	return 0;
}

string字符存取

#include<iostream>
#include<vector>
#include<string>
using namespace std;


void test01()
{
	string s1 = "hello world1";
	string s2 = "hello world";

	for (int i = 0; i < s1.size(); i++)
	{
		cout << s1[i] << " " ;
	}
	cout << endl;
	for (int i = 0; i < s1.size(); i++)
	{
		
		cout << s1.at(i) << " ";
	}
	cout << endl;
	for (int i = 0; i < s1.size(); i++)
	{
		s1[i] = i;
		cout << s1.at(i) << " ";
	}

	
}

int main()
{
	test01();

	system("pause");
	return 0;
}

string插入和删除

#include<iostream>
#include<vector>
#include<string>
using namespace std;


void test01()
{
	string s1 = "hello world1";
	
	s1.insert(1, "asdsa");
	cout << s1 << endl;

	s1.erase(1, 5);
	cout << s1 << endl;


}

int main()
{
	test01();

	system("pause");
	return 0;
}

string子串

#include<iostream>
#include<vector>
#include<string>
using namespace std;


void test01()
{
	string s1 = "hello world1";
	string s2 = s1.substr(2, 7);

	cout << s2 << endl;


}

// 
void test002()
{
	string email = "asdadafsfe3@qq.com";
	int position = email.find('@');
	string name = email.substr(0, position);
	cout << name << endl;
}
int main()
{
	test01();
	test002();
	system("pause");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值