STL——string

要写的都在代码里了: 

# include<iostream>
# include<algorithm>
# include<string> 
# include<stdexcept> 

using namespace std;

string str = "January February March";

# if 0
string模板库的参数一般都是:
(str)//整串 
(str,pos,n)//str中从pos开始的n个字符
(str,beg,end)//str从beg开始到end的区间
(str,n)//str的前n个
(str,n,c)//str的n个c
(p0,n0,str,pos,n)//str从pos开始的n个字符给源串的p0开始的n0个字符 
第一个空格下标为7
# endif

void test1()
{//初始化 
	string s0;
	s0.assign(str);//赋值 
	cout << "s0:" << s0 << endl;
	string s1("wwe!");//初始化 
	cout << "s1:" << s1 << endl;
	string s2(str);//string(str);
	cout << "s2:" << s2 << endl;
	string s3(str,8);//string(str,idx); 
	cout << "s3:" << s3 << endl;
	string s4(str,0,7);//string(str,beg,end); 
	cout << "s4:" << s4 << endl; 
	string s5(str,0,220);//string(str,beg,len);
	cout << "s5:" << s5 << endl;
	string s6(6,'a');
	cout << "s6:" << s6 << endl;//string(num,c); 
}

void test2()
{//赋值 
	string s1;
	s1.assign(str);//s.assign(str)
	cout << "s1:" << s1 << endl;
	string s2;
	s2.assign(str,0,7);//s.assign(str,pos,n)
	cout << "s2:" << s2 << endl; 
	string s3;
	s3.assign(5,'a');//s.assign(n,c)
	cout << "s3:" << s3 << endl;
}

void test3()
{//大小函数 
	string s(str);
	cout << "size:" << s.size() << endl;
	cout << "length:" << s.length() << endl;//引入length只是为了兼容c
	cout << "max_size:" << s.max_size() << endl;//返回string最大可包含的字符数 
}

void test4()
{//存取操作 
	string s(str);
	
	for(int i = 0;i < s.size();++i)
		cout << s[i];
	cout << endl;
	for(int i = 0;i < s.size();++i)
		cout << s.at(i);
	cout << endl;
	
	try{
		cout << s.at(100);
	}
	catch(out_of_range e)
	{
		cout << e.what() << endl;
	}
	
	# if 0
	区别在于at()函数会检查索引值的有效性,可能会抛出out_of_range的异常 
	# endif 
 } 

void test5()
{//compare比较操作 
	#if 0
	对于参与比较的两个字符串不能有NULL,否则,退出 
	对于比较,string类模板还支持><= >= <= == 的比较,不再赘述 
	#endif

	//ASCII a-97 A-65 0-48 
	string s1("January");
	string s2("jJanuary");
	
	cout << "January > jJanuary? :" << s1.compare(s2) << endl;
	cout << "January > January? :" << s2.compare(1,7,s1) << endl;
	cout << "January > January? :" << s2.compare(1,7,s1,0,s1.size());
 } 

void test6()
{//字符串的查找 
	string s(str);
	
	cout << "查找单个字符:" << endl;
	cout << "find()-a索引:" << s.find('a',5) << endl;//find从字符串首的第5个字符开始查找 
	cout << "rfind()-a索引:" << s.rfind('a') << endl;//rfind()从字符串的尾部开始查找,返回
	cout << "find_first_of('a'):" << s.find_first_of('a') << endl;//查找第一个 
	cout << "find_last_of('a'):" << s.find_last_of('a') << endl;//查找最后一个 
	cout << "查找字符串:" << endl;
	cout << "find-January:" << s.find('January') << endl;//返回的是待查找字符串最后一个字符在源串中的索引值
	cout << "rfind-January:" << s.rfind('January') << endl; 
 } 

    void replace_all(string &src,string old,int n0,string newStr){
        //用newStr全局替换old中的n0个字符
        int idx = -1;
        while(1){
            idx = src.find(old,idx+1);//从字符串的str[idx]开始查找old,返回找到的第一个
            if(idx == -1){
                break;
            }
            src.replace(idx,n0,newStr);
        }
    }

void test7()
{//insert erase append replace 
#if 0
这些操作都支持n0,p0,pos,n为参数的操作
不在赘述 
#endif
	 string s(str);
	 int idx = s.find(' ');
	 s.replace(idx,1,"1234567",3);//replace(p0,n0,s,pos,n)从p0开始的n0个字符用s的从pos开始的n个字符的子串代替 
        //结果为:January123February March
     s.replace(idx,2,"1234567",3);
     //结果为:January123ebruary March
	 cout << "第一个空格用*代替:" << s << endl;
	 s.erase(idx,3);//erase(pos,n)删除从pos开始的n个字符 
	 cout << "删除插入的字符:" << s << endl;
	 s.insert(idx," ");
	 cout << "在删除位置插入空格:" << s << endl; 
	 s.erase(idx,1);
	 s.insert(idx,3,'*');
	 cout << "插入*:" << s << endl;
	 s.append("!");
	 cout << "在串尾增加!:" << s << endl;
} 

void test8()
{//字符串截取 
	string s(str);
	string subStr = s.substr(0,7);//substr(pos,n)截取从pos 开始的n个字符 
	cout <<"字符串截取:" << subStr << endl; 
}

int main()
{
	//test1();//初始化 
	//test2();//assign 、赋值 
	//test3();//大小操作 
	//test4();//存取 
	//test5();//compare 
	//test6();//查找 
	//test7();//修改 
	test8(); 

 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AmosTian

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值