Lesson7

STL

string

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

void StringInit()
{
	string s1;
	string s2("helloworld");
	string s3(5,'q');
	string s4(s2);
	cout<<s1<<endl;
	cout<<s2<<endl;
	cout<<s3<<endl;
	cout<<s4<<endl;
}

void StringAt()
{
	string s1("helloworld");
	cout<<s1[3]<<endl;
	cout<<s1.at(3)<<endl;
}

void StringStr()
{
	string s1("skrskrskr?");
	const char *ptr=s1.c_str();
	cout<<ptr<<endl;
}

void StringCopy()
{
	string s1("skrskrskr");
	char buf[32]={0};
	s1.copy(buf,3,3);
	cout<<buf<<endl;
}

void StringLength()
{
	string s1("skrskrskr00");
	cout<<s1.length()<<endl;
	string s2;
	if(s2.empty())
	{
		cout<<"empty"<<endl;
	}
}

void StringAssign()
{
	string s1("skrskrskr");
	string s2=s1;
	cout<<s2<<endl;
	string s3;
	s3.assign(s2);
	cout<<s3<<endl;
	string s4;
	s4.assign("skkkekk",6);
	cout<<s4<<endl;
	string s5;
	s5.assign("aqaqaq");
	cout<<s5<<endl;
	string s6;
	s6.assign(5,'s');
	cout<<s6<<endl;
	string s7;
	s7.assign(s1,3,6);
	cout<<s7<<endl;
}

void StringAppend()
{
	string s1("hello");
	string s2("world");
	s1+=s2;
	cout<<s1<<endl;
	s2+="abc";
	cout<<s2<<endl;
	s1.append("hello");
	cout<<s1<<endl;
	s2.append("skrskrskr",6);
	cout<<s2<<endl;
	s1.append(s2);
	cout<<s1<<endl;
	s1.append(s2,2,4);
	cout<<s1<<endl;
	s2.append(3,'s');
	cout<<s2<<endl;
}

int main()
{
	StringInit();
	StringAt();
	StringStr();
	StringCopy();
	StringLength();
	StringAssign();
	StringAppend();
	return 0;
}

vector

#include <iostream>
#include <vector>

using namespace std;

void VectorInit()
{
	int array[]={1,2,3,4,5};
	vector<int> v1(array,array+5);
	vector<int> v2(v1.begin(),v1.end());
	vector<int> v3(v1.begin(),v1.begin()+3);
	vector<int> v4(3,9);
	vector<int> v5(v1);
	for (int i = 0; i < v1.size(); ++i)
	{
		cout<<v1[i]<<" ";
	}
	cout<<endl;
}

void VectorIterator()  //迭代器
{
	int array[]={1,2,3,4,5};
	vector<int> v1(array,array+5);
	for (std::vector<int>::iterator i = v1.begin(); i != v1.end(); ++i)  //遍历正向
	{
		cout<<*i<<" ";
	}
	cout<<endl;
	for (std::vector<int>::reverse_iterator ri = v1.rbegin(); ri != v1.rend(); ++ri)      //逆向遍历
	{
		cout<<*ri<<" ";		
	}
	cout<<endl;
}

void VectorSize()
{
	vector<int> v1(10);
	for(int i=0;i<v1.size();i++)
	{
		v1[i]=i+1;
	}
	v1.resize(15,10);
	for (std::vector<int>::iterator i = v1.begin(); i != v1.end(); ++i)  //遍历正向
	{
		cout<<*i<<" ";
	}
	cout<<endl;
	v1.resize(5);
	for (std::vector<int>::iterator i = v1.begin(); i != v1.end(); ++i)  //遍历正向
	{
		cout<<*i<<" ";
	}
	cout<<endl;
}

void VectorInsert()
{
	vector<int> v1(10);
	for(int i=0;i<v1.size();i++)
	{
		v1[i]=i+1;
	}
	v1.insert(v1.begin(),4,6);
	for (std::vector<int>::iterator i = v1.begin(); i != v1.end(); ++i)  //遍历正向
	{
		cout<<*i<<" ";
	}
	cout<<endl;
	v1.push_back(1);
	for (std::vector<int>::iterator i = v1.begin(); i != v1.end(); ++i)  //遍历正向
	{
		cout<<*i<<" ";
	}
	cout<<endl;
	v1.pop_back();
	for (std::vector<int>::iterator i = v1.begin(); i != v1.end(); ++i)  //遍历正向
	{
		cout<<*i<<" ";
	}
	cout<<endl;
}


int main()
{
	VectorInit();
	VectorIterator();
	VectorSize();
	VectorInsert();
	return 0;
}

deque

#include <iostream>
#include <deque>

using namespace std;

int main()
{
	deque<int> d1(10);
	for(int i=0;i<d1.size();i++)
	{
		d1[i]=i+1;
	}
	d1.insert(d1.begin()+5,2,6);
	for (std::deque<int>::iterator i = d1.begin(); i != d1.end(); ++i)  //遍历正向
	{
		cout<<*i<<" ";
	}
	cout<<endl;
	d1.push_back(1);
	d1.push_front(2);
	for (std::deque<int>::iterator i = d1.begin(); i != d1.end(); ++i)  //遍历正向
	{
		cout<<*i<<" ";
	}
	cout<<endl;
	d1.pop_back();
	d1.pop_front();
	for (std::deque<int>::iterator i = d1.begin(); i != d1.end(); ++i)  //遍历正向
	{
		cout<<*i<<" ";
	}
	cout<<endl;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值