1. 程式人生 > >C++STL容器---String字元容器

C++STL容器---String字元容器

C語言只是提供了Char來處理字元,對於字串來說,只能通過字串陣列來處理,十分不方便.C++ STL提供了一個string容器來處理字串,可以進行新增和刪除和替換等等。用我們家鄉話來說就是“真不孬!"

一、操作函式

s.length();//返回字串的長度

s.append();//在尾部新增相應的字串

s.insert(index,字元);//向字串s的某個位置插入特定字元

s.empty();//判斷字串是否為空

s.replace(位置,距離,“替換的字串");//從特定位置開始將特定距離的字串替換為字串

s.find('字元');//在s中尋找字元的位置

s.compare("字串");//相等返回0,如果大於,返回1,小於返回-1

reverse(s.begin(),s.end());//反向排序,倒敘

s.assign(str2,4,3);//將str2的索引為4距離為3的子串賦值給s

s.erase(索引開始,距離);//刪除相對應的子串

s.swap(str);//將兩個字串交換

二、程式碼解釋

1、對於erase和length使用

#include<string>
#include<iostream>
using namespace std;
int main()
{
  string s("123456");
  cout<<"字串的長度"<<s.length()<<endl;
    s.erase(3);
    cout<<"當前的字串內容為:"<<s<<endl;
    cout<<"當前字串的長度為:"<<s.length()<<endl;
    s="123456";
    s.erase(3,1);//位置+距離
    cout<<"使用erase(3,1)的操作結果"<<endl;
       cout<<"當前的字串內容為:"<<s<<endl;
    cout<<"當前字串的長度為:"<<s.length()<<endl;
  return 0;
}

執行結果為:

 

2、對於replace函式的使用

#include<string>
#include<iostream>
using namespace std;
int main()
{
  string s("123456");
    cout<<"當前的字串內容為:"<<s<<endl;
   s.replace(3,3,"nihao");
    cout<<"使用replace(3,3,"<<"nihao)"<<"的操作結果"<<endl;
       cout<<"當前的字串內容為:"<<s<<endl;
    cout<<"當前字串的長度為:"<<s.length()<<endl;
  return 0;
}

執行結果為:

3、對於find函式的使用

#include<string>
#include<iostream>
using namespace std;
int main()
{
  string s("123456");
    cout<<"當前的字串內容為:"<<s<<endl;
    cout<<"2的位置"<< s.find('2')<<endl;
    cout<<"4的位置"<<s.find('4')<<endl;
    cout<<s.compare("1234567")<<endl;
  return 0;
}

執行結果:

4、對於字串的新增函式append和insert

#include<string>
#include<iostream>
using namespace std;
int main()
{
  string s("123456");
    cout<<"當前的字串內容為:"<<s<<endl;
     s.append("78");
     s.insert(0,"0");
     cout<<"修改之後的字串內容為:"<<s<<endl;
  return 0;
}

執行結果:

5、對於翻轉函式reverse的使用

#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
  string s("123456");
    cout<<"當前的字串內容為:"<<s<<endl;
     reverse(s.begin(),s.end());
     cout<<"修改之後的字串內容為:"<<s<<endl;
  return 0;
}

執行結果:

三、其他方面

對於使用printf函式輸出字串應該使用s.c_str()

#include<string>
#include<math.h>
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
  string s;
  char ss[100];
  scanf("%s",&ss);
  s=ss;
  printf("%s",s.c_str());
  cout<<endl;
  printf("%s",ss);
  cout<<endl;
  cout<<s<<endl;
  return 0;
}

同時string 又可以作為vector的向量元素

#include<string>
#include<math.h>
#include<iostream>
#include<stdio.h>
#include<vector>
using namespace std;
int main()
{
 vector<string> v;
 v.push_back("zhangyang");
 v.push_back("hoho");
 v.push_back("1232");
   cout<<v[0]<<endl;
   cout<<v[1]<<endl;
   cout<<v[2]<<endl;
}

sscanf可以將字串格式化分割

#include<string>
#include<math.h>
#include<iostream>
#include<stdio.h>
#include<vector>
using namespace std;
int main()
{
    string s1,s2,s3;
    char sa[100],sb[100],sc[100];
    sscanf("abc 123 pc","%s %s %s",sa,sb,sc);
    s1=sa;
    s2=sb;
    s3=sc;
    cout<<s1<<" "<<s2<<" "<<s3<<" "<<endl;
    int a,b,c;
    sscanf("1 2 3","%d %d %d",&a,&b,&c);
    cout<<a<<" "<<b<<" "<<c<<endl;
    int x,y,z;
    sscanf("4,5$6","%d,%d$%d",&x,&y,&z);
    cout<<x<<" "<<y<<" "<<z<<endl;
    return 0;

}

字串與數字的轉換

#include<iostream>
#include<cstdio>
#include<string>
#include<sstream>
using namespace std;
string convertTostring(double x)
{
  ostringstream o;
  if(o<<x)
    return o.str();
  return "conversion error";

}
double convertFromString(const string &s)
{
 istringstream i(s);
  double x;
  if(i>>x)
    return x;
  return 0.0;
}

int main()
{
     char b[10];
     string a;
     sprintf(b,"%d",1975);
     a=b;
     cout<<a<<endl;
     string cc=convertTostring(1976);
     cout<<cc<<endl;
     string dd="2006";
     int p=convertFromString(dd)+2;
     cout<<p<<endl;
     return 0;
}