C++ STL-string实例

vector与iterator

#if 0
/*容器(vector)与迭代器(iterator)循环查找*/
#include<iostream>
#include<vector>//STL向量容器
#include<algorithm>//STL算法

using namespace std;

int main()
{
	vector<int> a;//声明一个变量(向量容器,可以代替数组,动态数组)
	a.push_back(11);
	a.push_back(22);
	a.push_back(33);
	a.push_back(444);

	//输出向量里面的数据
	vector<int>::iterator i=a.begin();//使用向量迭代器
	//循环
	while(i!=a.end())
	{
		//迭代器实际为指针
		cout<<"This is:"<<*i<<endl;
		++i;//迭代器
	}

	//STL算法进行查找,begin-end
	vector<int>::iterator element=find(a.begin(),a.end(),225);//查找22
	//如果没找到则等于a.end()
	if(element!=a.end())//已经找到
	{
		int position=distance(a.begin(),element);//下标
		cout<<"value:"<<*element<<endl;
		cout<<"位置:"<<position<<endl;
	}
	else
	{
		cout<<"Find not the value..."<<endl;
	}

	return 0;
}
#endif

STL-string

#if 0
/*STL-string*/
#include<iostream>
#include<string>

int main()
{
	/*C-string,并不是真正意义的字符串
	char xc[20]="sw";
	char *xc1="sw";*/

	using namespace std;
	//std::string
	string	strName("小明");
	cout<<"str:"<<strName<<endl;

	string strName1="小红";
	cout<<"str1:"<<strName1<<endl;

	//定义C风格字符串
	const char* strc="hello string";
	string strcpp(strc);//将c风格的字符串转换为c++风格的字符串
	cout<<"strcpp:"<<strcpp<<endl;

	//声明Cpp风格字符串
	string strcpp1("hello strig");
	string strcpp2(strcpp1);//将cpp1作为参数传递给cpp2,相当于strcpy
	cout<<"strcpp1:"<<strcpp1<<endl;
	cout<<"strcpp2:"<<strcpp2<<endl;
	
	string strcpp3(strc,5);//复制strc前五个
	cout<<"strcpp3:"<<strcpp3<<endl;

	//初始化为10个a
	string sw(10,'a');
	cout<<"sw:"<<sw<<endl;

	//c string copy
	const char *sw="hello c string...";
	char *xc=new char(strlen(sw)+1);//初始化大小
	strcpy(xc,sw);//将sw复制到xc
	//delete[] xc;//释放

}
#endif

string常见操作

#if 0
/*STL-string常见操作*/
#include<iostream>
#include<string>
//#include<iterator>//迭代器
using namespace std;
int main()
{
	string str1("hello string");//初始化
	//cout<<"str1:"<<str1<<endl;
	/*用传统方法显示字符串的每一个字符*/
	cout<<"传统方法:"<<endl;
	for(size_t i=0;i<str1.length();++i)
	{
		cout<<str1[i]<<endl;
	}

	/*使用迭代器(iterator)*/
	cout<<"iterator:"<<endl;
	string::const_iterator itr;
	for(itr=str1.begin();itr!=str1.end();++itr)
	{
		cout<<*itr<<endl;
	}
	cout<<str1.c_str()<<endl;//得到C语言的字符串
	return 0;
}
#endif

字符串连接

#if 0
/*使用Cpp字符串进行连接*/
#include<iostream>
#include<string>
using namespace std;

int main()
{
	string str1("hello");
	string str2("string");
	str1+=str2;//连接字符串
	cout<<"link:"<<str1<<endl;
	
	string str3("asdasd");
	str1.append(str3);//link-追加
	cout<<"str1:"<<str1<<endl;

	const char* str4="swxctx";//C
	str1.append(str4);
	cout<<"str1:"<<str1<<endl;

	return 0;
}
#endif

查找

#if 0
/*查找*/
#include<iostream>
#include<string>
using namespace std;

int main()
{
	string str1("Good evening,hello,swxctx,hello!");
	cout<<str1<<endl;

	//查找
	size_t noffset=str1.find("hello",0);//从0处开始查找hello
	if(noffset!=string::npos)
	{
		cout<<"success<index>:"<<noffset<<endl;
	}
	else
	{
		cout<<"failed"<<endl;
	}
	cout<<endl;

	//查找多个
	size_t noffset1=str1.find("hello",0);//从头查找
	while(noffset1 != string::npos)//一直循环查找
	{
		cout<<"success<index>:"<<noffset1<<endl;
		size_t noffset2=noffset+1;//第一次找到后位置加1
		noffset1=str1.find("day",noffset2);//查找第二个

	}
	cout<<endl;

	/*查找所有的o*/
	size_t offseta1=str1.find('o',0);//从头开始查找o
	while(offseta1 != string::npos)
	{
		cout<<"success<index>:"<<offseta1<<endl;	
		size_t offseta2=offseta1+1;
		offseta1=str1.find('o',offseta2);//从第二次位置开始(找到第一个以后)
	}
	return 0;
}
#endif

截短

#if 0
/*截短*/
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int main()
{
	string str1("hello string!This is a beautiful day..");
	cout<<"The string is:"<<str1<<endl;
	cout<<endl;

	//截短
	str1.erase(13,17);//13-17截断
	cout<<"new(13-17):"<<str1<<endl;
	cout<<endl;

	//find,迭代器
	string::iterator strfin=find(str1.begin(),str1.end(),'s');
	if(strfin!=str1.end())
	{
		str1.erase(strfin);//将找到的删除
		cout<<"delect str1:"<<str1<<endl;
		cout<<endl;
	}

	//delect all
	str1.erase(str1.begin(),str1.end());
	if(str1.length()==0)
		cout<<"The string is empty..."<<endl;
	cout<<endl;
	return 0;
}
#endif

反转

#if 0
/*字符串反转*/
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int main()
{
	string str1("Hello string!We will reverse you.");
	cout<<"str1:"<<str1<<endl;
	cout<<endl;

	//reverse反转
	reverse(str1.begin(),str1.end());
	cout<<"new str1:"<<str1<<endl;
	cout<<endl;

	return 0;
}
#endif

大小写转换

/*字符转换*/
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
	cout<<"Please input a line string1:";
	string strInput;
	getline(cin,strInput);//读取
	//转变为大写
	transform(strInput.begin(),strInput.end(),strInput.begin(),toupper);
	cout<<"after:"<<strInput<<endl;
	
	//转变为小写
	transform(strInput.begin(),strInput.end(),strInput.begin(),tolower);
	cout<<"after str:"<<strInput<<endl;
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值