STL(Standard Template Library,标准模板库)

       STL的从广义上讲分为三类:algorithm(算法)、container(容器)和iterator(迭代器),容器和算法通过迭代器可以进行无缝地连接。

                                                                                   

容器:用来管理一组元素

容器分类:vector(向量)、deque(列表)、list(双队列)

算法:主要由头文件<algorithm>,<numeric>和<functional>组 成

迭代器:迭代器在STL中用来将算法和容器联系起来,起着一种黏和剂的作用。

string是STL的字符串类型,通常用来表示字符串。

#include <iostream>
#include <exception>

using namespace std;

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

void StringAt()
{
	string s1("123455677");
	cout<<s1[1]<<endl;
	cout<<s1.at(1)<<endl;
	
	//cout<<s1[1000000]<<endl;
	
	try
	{
		cout<<s1.at(10)<<endl;
	}
	catch(exception &e)
	{
		cout<<e.what()<<endl;
	}
}

void StringStr()
{
	string s1("iiiiiiiii");
	const char *ptr =s1.c_str();	 //返回一个以'\0'结尾的字符串的首地址
	cout<<ptr<<endl;
}

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

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

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

执行结果:

猜你喜欢

转载自blog.csdn.net/ShiHongYu_/article/details/81434579