STL容器(未完成

一、string


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


/*
assign , append , find , rfind , replace , compare , at , 
insert , erase , 
string substr(int pos = 0, int n = npos) const; 返回由pos开始的n个字符组成的字符串
*/

int main()
{
	string s1 = "jay";
	string s2;
	s2.assign("zhou");
	cout << s1 <<"  "<<s2 << endl;

	string s3 = s1 +" " + s2;
	s3.append("QAQ");
	cout << s3 << endl;

	string s4 = "aabbccaa";
	int ite = s4.find("a");
	cout << ite << endl;

	ite = s4.rfind("a");
	cout << ite << endl;

	s4.replace( 1 , 2, "c");//第几个,几个字符
	cout << s4 << endl;

	int bl = s1.compare(s2);

	if (bl > 0)
	{
		cout << "s1>s2" << endl;
	}
	else if (bl == 0)
	{
		cout << "s1 = s2" << endl;
	}
	else cout << "s1 < s2" << endl;

	cout << s4.at(3) << endl;

	string s5 = "abaaba";
	s5.insert( 4 ,3,'s');
	cout << s5 << endl;

	s5.erase(s5.begin(),s5.end());//==s5.clear()
	cout << s5 << endl;
	s4.erase(3, 4);

	string s6 = "zhouzhouzhou";
	string s7;
	s7 = s6.substr(0, 8);
	cout << s7 << endl;

	system("pause");
	return 0;

}

二、vector

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

//assign ,empty , capacity , size , resize , push_back , pop_back , 
//insert, erase, clear, at, front, back, swap, reserve,

void print(vector<int>& v)
{
	for (auto a : v)
	{
		cout << a <<" ";
	}
	cout << endl;
}

void test01()
{
	vector<int>v1(10,2);
	print(v1);
	v1.assign(10, 1);
	print(v1);
	vector<int>v2(v1.begin(), v1.begin() + 3);
	print(v2);
}

void test02()
{
	vector<int>v1;
	v1.resize(3, 2);
	if (v1.empty())
	{
		cout << "v1为空" << endl;
	}
	else cout <<"v1的容量为  :"<< v1.capacity() << endl;

	vector<int>::iterator const ite = (v1.begin()+1);
	v1.insert(ite, 3, 3);
	print(v1);
	v1.erase(v1.begin() + 1, v1.end() - 1);
	print(v1);
}

void test03()
{
	vector<int>v1;
	for (int i = 0; i < 99; i++)
	{
		v1.push_back(i);
	}
	cout << v1.capacity() << endl;
	//收缩内存
	vector<int>(v1).swap(v1);
	cout << v1.capacity() << endl;

	vector<int>v2;
	//预留空间但无法访问
	v2.reserve(5);
	for (size_t i=0 ;i<v2.capacity(); i++)
	{
		v2.push_back(i);
	}
	print(v2);
}

int main()
{
	//test01();

	//test02();

	//test03();
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值