STL标准模版库字符串的用法(下)

贴些各种函数使用的代码:

// string_.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	string var("abcdefghijklmn");
	const string dest("1234");
	string dest2("567891234");
	//替换
	var.replace(4,3,dest);//从源串的4位置替换3个字符,用dest的所有字符串来代替
	cout<<"替换后的字符串为:"<<var<<endl;
	var.replace(3,1,dest.c_str(),1,3);//把源串的3位置替换1个字符,用dest的1-3位来替换
	cout<<"替换后的字符串为:"<<var<<endl;
	var.replace(3,1,5,'X');//用5个‘X’来替换源串3位置的1个元素
	cout<<"替换后的字符串为:"<<var<<endl;
	string::iterator itA, itB;
	itA=var.begin();
	itB=var.end();
	var.replace(itA,itB,dest);//使用迭代器来替代,把var的开始到结束全部替换
	cout<<"替换后的字符串为:"<<var<<endl;
	string::iterator itC, itD;
	itC=dest2.begin()+1;
	itD=dest2.end();
	var.replace(itA,itB,itC,itD);
	cout<<"替换后的字符串为:"<<var<<endl;
	var.replace(3,1,dest.c_str(),4);//限定字符
	cout<<"替换后的字符串为:"<<var<<endl;
	//字符串联接
	string str1("Inter");
	string str2("national");
	string str3=str1+str2;
	cout<<"str3=str1+str2="<<str3<<endl;
	//输入一行字符
	string s,s1,s2;
	cout<<"请输入一个字符串:";
	cin>>s;
	cout<<"输入的字符串为:"<<s<<endl;
	getchar();
	cout<<"请输入一个字符串:";
	getline(cin,s1);//以回车为结束
	cout<<"输入的字符串为:"<<s1<<endl;
	cout<<"请输入一个字符串:";
	//getchar();
	getline(cin,s2,' ');//以空格为结束
	cout<<"输入的字符串为:"<<s2<<endl;
	//字符串搜索
	string str_ch("for");
	string str("Hi, Peter, I'm sick. Please bought some drugs for me.");
	string::size_type m=str.find('P',5);//第一个输入为要查找的字母,第二个参数为从第5个字符还是查找
	string::size_type rm=str.rfind('P',5);//rfind与find不同之处是从后往前查找,
	cout<<"Example-find():The position (forward) of 'P' is: "<<(int) m<<endl;
	cout<<"Example-rfind():The position (reverse) of 'P' is: "<<(int) rm<<endl;
	string::size_type n=str.find("some",0);//第一个输入为要查找的字符串,第二个参数为从第0个字符还是查找
	string::size_type rn=str.rfind("some",0);//rfind与find不同之处是从后往前查找,
	cout<<"Example-find():The position (forward) of 'some' is: "<<(int) n<<endl;
	cout<<"Example-rfind():The position (reverse) of 'some' is: "<<(int) rn<<endl;
	string::size_type mo=str.find("drugs",0,5);//第一个输入为要查找的字母,第二个参数为从第5个字符还是查找
	string::size_type rmo=str.rfind("drugs",0,5);//rfind与find不同之处是从后往前查找,
	cout<<"Example-find():The position (forward) of 'drugs' is: "<<(int) mo<<endl;
	cout<<"Example-rfind():The position (reverse) of 'drugs' is: "<<(int) rmo<<endl;
	string::size_type no=str.find(str_ch,0);//第一个输入为要查找的字母,第二个参数为从第5个字符还是查找
	string::size_type rno=str.rfind(str_ch,0);//rfind与find不同之处是从后往前查找,
	cout<<"Example-find():The position (forward) of 'for' is: "<<(int) no<<endl;
	cout<<"Example-rfind():The position (reverse) of 'for' is: "<<(int) rno<<endl;
	//find_first_of()
	//find_last_of()
	//find_first_not_of()
	//find_last_not_of()
	int length=str.length();
	string::size_type mm=str.find_first_of('P',0);//查找第一个字符'P'
	string::size_type rmm=str.find_last_of('P',(length-1));//查找最后一个字符P
	cout<<"Example-find_first_of():The position (forward) of 'P' is: "<<(int) mm<<endl;
	cout<<"Example-find_last_of():The position (reverse) of 'P' is: "<<(int) rmm<<endl;
	string::size_type mmo=str.find_first_of("some",0);//查找第一个字符串some
	string::size_type rmmo=str.find_last_of("some",(length-1));//查找最后一个字符串some
	cout<<"Example-find_first_of():The position (forward) of 'some' is: "<<(int) mmo<<endl;
	cout<<"Example-find_last_of():The position (reverse) of 'some' is: "<<(int) rmmo<<endl;
	string::size_type mmol=str.find_first_of("drugd",0,4);//查找第一个字符串some
	string::size_type rmmol=str.find_last_of("drugd",(length-1),4);//查找最后一个字符串some
	cout<<"Example-find_first_of():The position (forward) of 'drugs' is: "<<(int) mmol<<endl;
	cout<<"Example-find_last_of():The position (reverse) of 'drugs' is: "<<(int) rmmol<<endl;
	string ss="forforjiangfor";
	string::size_type mmoln=ss.find_first_not_of(str_ch,0);//查找第一个不是for字符的位置
	string::size_type rmmoln=ss.find_last_not_of(str_ch,(length-1));//查找最后一个不是for的字符串
	cout<<"Example-find_first_not_of():The position (forward) of 'for' is: "<<(int) mmoln<<endl;
	cout<<"Example-find_last_not_of():The position (reverse) of 'for' is: "<<(int) rmmoln<<endl;
	//迭代器的使用方法
	string sstr("The zip code of Hondelage in Germany is 38108.");
	cout<<"Original: "<<sstr<<endl;
	string sd(sstr.begin(),sstr.end());
	cout<<"Destination: "<<sd<<endl;
	transform(sstr.begin(),sstr.end(),sd.begin(),toupper);
	cout<<"Destination (All Toupper) : "<<sd<<endl;
	string sd1;
	sd1.append(sd.begin(),(sd.end()-7));
	cout<<"Destination sd1: "<<sd1<<endl;
	//reverse_iterator
	string sd2;
	string::reverse_iterator iterA;
	string temp="0";
	for(iterA=sd.rbegin(); iterA!=sd.rend();iterA++)
	{
		temp=*iterA;
		sd2.append(temp);
	}
	cout<<"Destination sd2: "<<sd2<<endl;
	sd2.erase(sd2.begin(),(sd2.end()-7));//erase()函数中使用迭代器
	cout<<"Destination sd2(Erase some chars): "<<sd2<<endl;
	string::iterator iterB=sd2.begin();
	string sd3=string("123456");
	sd2.insert(sd2.begin(),sd3.begin(),sd3.end());//insert()函数中使用迭代器
	cout<<"Destination sd2(Insert some chars): "<<sd2<<endl;
	sd2.replace(sd2.begin(),sd2.end(),"This is an Example of Replace!");
	cout<<"Destination sd2(Replace All): "<<sd2<<endl;//replace()函数使用迭代器
	system("pause");
	return 0;
}
结果:




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值