c++ STL 之 string

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013919153/article/details/82378083

template<class T>
void print_elemnt(T &v)
{
	for(auto i : v)
		cout<<i<<" ";
	cout<<endl;
}

void test_string()
{
	/*
	 string的本质,处理utf-8字符
	 typedef basic_string<char> string;
	 */ 

	char tmp[] = "\x31\x32\x33\x34\x35\x36\x37";
	string a(tmp);		//字符串赋值初始化
	string b(tmp,5);	//取前n个字符初始化
	string c(3,0x41);	//n个val初始化
	string d(a.begin(),a.begin()+4);	//区间初始化[,)
	string e(a);		//复制构造函数
	string f(a,2,3);	//取a中第二个位置开始的3个字符
	string g;			//不能这样写,因为没有对应构造函数 string g = 'm';
	g = 'm';
	string h = f;
	
	/**
	 关于迭代器遍历
	 遍历string中的char,注意删除时迭代器的失效问题
	 **/

	/**
	 关于大小、容量、再分配等
	 size() 和 length() 返回字符总数
	 max_size() 返回最大存储个数,视系统而定,不固定
	 capacity() 当前可用容量

	 resize(size_t n, char c) 和 reserve()
	 前者重新分配大小为n个数据内存,如果n大于原始空间大小,用默认值或者特定值填写大于部分;如果小于,取前n个数据
	 后者如果n大于原始内存,重新分配;如果n小于原始内存,不做任何操作。
	 **/

	//访问数据的方式
	cout<<"The third element in a is "<<a[2]<<endl;	
	cout<<"The forth element in a is "<<a.at(3)<<endl;
	cout<<"first elemnt is "<<a.front()<<endl;
	cout<<"last element is " <<a.back()<<endl;

	//数据操作方式
	g.append(tmp);	//操作方法和构造函数参数差不多
	g += 'a';
	g += a;
	g += "fdsaf";
	cout<<"g:"<<endl;
	g.push_back('x');	//尾部添加一个元素
	print_elemnt(g);
	g.pop_back();		//尾部删除一个元素

	g.erase(2,5);		//从索引2开始删除5个字符 或者 删除一个迭代器指向的字符 或者 一个区间[,)
	g.insert(2,2,'x');	//插入元素,一个字符 、一个区间 、重复插入一个字符、string对象或者其中的一部分
	h.clear();			//清空元素
	if (h.empty())		//判断是否为空
	{
		cout<<"h is empty"<<endl;
	}
	
	//g.swap(h);		//交换两个string对象内容
	cout<<" g: "<<g.c_str()<<endl; //获取string对象的c字符串,即以null(0)结尾。
	cout<<" g: data--"<<g.data()<<endl;		//和c_str()没啥区别,好多人说没有"\0",但是我看了英文文档,有"\0";其实主要看具体库的实现。
	
	//size_t copy (char* s, size_t len, size_t pos = 0) const; //将string内容从POS开始len长度字符拷贝到s指针指向的内存中。
	g.find('x',0);		//在string对象中从某一个位置开始,查找一个或者一段字符第一次出现的地方,返回第一个字符对应索引;否则返回string::npos
	g.rfind("ab");		//与find类似,查找最后一次出现的位置

	h = "12345654";
	cout<<"find :"<<endl;
	cout<<h.find_first_of("49988")<<endl;	//搜索string对象中的内容第一次出现在参数字串中的位置 本例返回3
	cout<<h.find_first_not_of("49988")<<endl;	//搜索string对象中的内容第一次没有在参数字串中出现的位置 本例返回0
	cout<<h.find_last_of("49988")<<endl;		//搜索最后一次在参数字串中出现的位置	(查找存在字符)	本例返回7
	cout<<h.find_last_not_of("49988")<<endl;	//搜索最后一次在参数字串中没有出现的位置	(查找不存在字符)	本例返回6
	cout<<"h:substr=="<<h.substr(3,4)<<endl;	//字串操作,从位置3开始长度为4的字符串构造一个string对象返回

	if (g.compare(h) != 0)		//比较字串,可以比较其中一部分,参数:npos1,len,desString,npos2,len
	{
		cout<<"g is not equal to h"<<endl;
	}

	//利用algorithm文件进行大小写转换
	transform(g.begin(),g.end(),g.begin(),::toupper);
	cout<<"convert g to upper: "<<endl;
	print_elemnt(g);
	transform(g.begin(),g.end(),g.begin(),::tolower);

	//读取内容到string
	getline(std::cin,h);	//从流中读取内容,直到遇到指定字符为止,默认'\n';错误
	cout<<"input contents:"<<endl;
	print_elemnt(h);

	//显示各元素内容
	print_elemnt(tmp);
	cout<<"a:"<<endl;
	print_elemnt(a);
	cout<<"b:"<<endl;
	print_elemnt(b);
	cout<<"c:"<<endl;
	print_elemnt(c);
	cout<<"d:"<<endl;
	print_elemnt(d);
	cout<<"e:"<<endl;
	print_elemnt(e);
	cout<<"f:"<<endl;
	print_elemnt(f);
}

猜你喜欢

转载自blog.csdn.net/u013919153/article/details/82378083