STL学习之string类

1.string 与char *的相互转化

char * to string 可通过构造函数直接转化

strings1="123456";

string to char *,可通过string类的c_str()函数获得char *的地址

    const char *str = s1.c_str();
 
如下:
·void main1()
·{
    ·string s1 = "123456";
   · const char *str = s1.c_str();
  ·  cout <<str;
·}
输出结果为123456;
 
 
2.string类的遍历
  方法1:数组遍历
 	for (int i=0; i<s1.length(); i++)
	{
		cout << s1[i] << " ";
	}
  方法2:迭代器遍历
 	for(string::iterator it = s1.begin();it != s1.end();it++)
    	{
      		  cout<<*it<<" ";
    	}

如下:
	void main2()
	{
   	 string s1("123");
  	  //数组的方式遍历字符串
   	 for(size_t i = 0; i < s.length();i++)
  	  {
  	      cout<< s1[i] <<" ";
  	  }
   	 //通过迭代器遍历
	    for(string::iterator it = s1.begin();it != s1.end();it++)
  	  {
    	   	cout<<*it<<" ";
       	  }
	}
3.string 类的连接
  方法1:string类重载了‘+’运算符,所以可以直接使用srt1 + str2;
	
    string s1 = "123";
    string s2 = "abc";
    s1 = s1 + s2;
    cout<<s1;
  输出结果为123abc
  
  方法2 :实用string类的append()函数,可以实现字符串的连接;
  
    s1.append(s2);//直接append string
    cout <<s1<<endl;
    s1.append(s2.begin(),s2.end());//通过迭代器位置来append
    cout<<s1<<endl;
如下:
   
	void main3()
	{
  	  string s1 = "123";
  	  string s2 = "abc";
  	  s1 = s1 + s2;
  	  cout<<s1<<endl;
  	  s1.append(s2);
  	  cout <<s1<<endl;
   	  s1.append(s2.begin(),s2.end());
   	  cout<<s1<<endl;
	}
	
 
4.字符串的查找和替换
  string 类的find()函数,可以返回想要查找的字符串的位置
  
    string s1 = "hello wbm 111  wbm 222  wbm 333 ";
    int index = s1.find("wbm",0);//查找的字符串,启示位置,返回第一次的结果index位置
    cout<<index<<endl;
    输出结果为 6,“wbm”的第一次出现的位置为6
   案例一:查找指定字符串的个数
   
   string s1 = "wbm hello wbm 111  wbm 222  wbm 333";
      int count = 0;
      int offindex = s1.find("wbm",0);
      while (offindex != string::npos)
      {
          count ++;
          offindex = s1.find("wbm",offindex+1);
      }
      cout <<count<<endl;
    输出结果为4
  案例二:将查找到的字符串转化为大写
  
   string s1 = "wbm hello wbm 111  wbm 222  wbm 333";
     int offindex = s1.find("wbm",0);
     while(offindex != string::npos)
     {
         s1.replace(offindex,3,"WBM"); 
         offindex = s1.find("wbm",offindex += 1);
     }
     cout<<s1<<endl;用到了replace函数,replace ()函数第一个参数是位置,第二个参数是长度,第三个参数是需要替换成的字符串
 
5.//截断(区间删除)和插入
  删除操作:
 string s1 = "hello1 hello2 hello1";
    string::iterator it = find(s1.begin(),s1.end(),'1'); //find的算法
    if(it != s1.end())
        s1.erase(it);//t通过erase来删除,需要获取到迭代器的位置
    cout<<s1<<endl;
  插入操作: 
    string s2 = "BBB";
    s2.insert(0, "AAA"); // 头插法,制定位置插入
    s2.insert(s2.length(), "CCC");//在结尾插入,制定位置插入
6.字符串的大小写转换
    string s1 = "AAAbbb";
    //1函数的入口地址 2函数对象 3预定义的函数对象
    transform(s1.begin(), s1.end(),s1.begin(), ::toupper);
    cout << "s1" << s1 << endl;
    string s2 = "AAAbbb";
    transform(s2.begin(), s2.end(), s2.begin(), ::tolower);
    cout << "s2:" << s2 << endl;
  

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值