C++ 標準庫string【學習筆記】

C++ 標準庫string

基礎操作

#include <string>
#include <iostream>
#include <cctype>

using namespace std;

int main()
{
    // 初始化------------------------------------------------------
    string s1 = "value_s1";
    string s2(s1);
    string s3("value_s3");
    string s4(4,'c');
    
    cout << "s1 = " << s1 << endl;  // s1 = value_s1
    cout << "s2 = " << s2 << endl;  // s2 = value_s1
    cout << "s3 = " << s3 << endl;  // s3 = value_s3
    cout << "s4 = " << s4 << endl;  // s4 = cccc

    // 操作----------------------------------------------------------
    cout << "size of s1:" << s1.size() << endl;
    cout << "s2 is empty?:" << s2.empty() << endl;
    cout << "s1 + s2 = :" << s1+s2 << endl;

    // 讀取字符串:-----------------------------------------------------------------------------------------------
    // IO操作符讀取:讀取從非空格開始,到空格結束
    string a,b;
    cout << "input string:" << endl;
    cin >> a >> b;
    cout << "a = " << a << "b = " << b << endl;
    getline從流中讀取: 讀取一整行
    string c;
    while (getline(cin, c)){
        cout << "c = " << c << endl;
    }

    // 使用範圍for語句遍歷string:----------------------------------------------------------------------------------
    string str="ab!!cd,,efg";
    for(auto c: str){
        cout << c <<endl;
    }
    // 使用範圍for語句遍歷string,進行操作
    cout << "使用範圍for語句遍歷string:"<<endl;
    decltype(str.size()) count = 0; // decltype(str.size())返回的是str.size()的類型
    // 當不需要更改str中的值時:
    for(auto c:str){
        if(ispunct(c)) //ispunct函數是<cctype>頭文件中的字符操作函數
            count++;
    }
    // 當需要更改str中的值時:使用引用
    for(auto &c:str){
        c = toupper(c);  //c是引用,因此這裏會直接更改str中的值
    }
    cout <<"number of puncts:"<< count << endl;
    cout << "str = "<<str<<endl;

    // 字符串中字符的訪問:-----------------------------------------------------------------------------------
    // 1. 下標訪問:
    cout << "下標訪問" <<endl;
    for(int i = 0; i!=str.size();i++){
        cout << str[i] <<endl;
    }
    // 2. 迭代器訪問:
    cout << "迭代器訪問" <<endl;
    for(auto it=str.begin(); it!=str.end(); ++it){
        cout<<*it<<endl;  // it是一個迭代器變量,*it返回所指元素的引用,及在*it上的操作也是在str中的操作
    }
    return 0;
}

進階操作

構造、初始化string

  • string s(cp,n) 從數組中拷貝前n個字符
  • string s(s2,pos2) 從字符串s2的pos2位置開始拷貝
  • string s(s2,pos2,len2) 從字符串s2的pos2位置拷貝len2個字符
const char *cp = "hello world!!";
char p[]={'h','w'};
string s1(cp); //直接從字符串數組中拷貝,直到遇到空字符 s1="hello world!!"
string s2(p); //錯誤,因爲p不是以空字符結束,無法拷貝
string s3(p,2); //正確,拷貝數組p的前兩個字符 s3="hw"
string s4(cp+6,5);//從cp+6位置開始拷貝5個字符 s4="world"
string s5(s1,6,5);//從s1的位置6開始拷貝5個字符 s5="world"
string s6(s1,6);//從s1的位置6開始拷貝 s6="world!!"
string s7(s1,6,20);//從s1的位置6開始拷貝20個字符 s7="world!!!"
string s8(s1,16);//從s1的位置16開始拷貝,拋出out_of_range異常
  • s.substr(pos,n)從string的pos位置開始拷貝n個字符
string s("hello world");
string s2=s.substr(0,5); //s2="hello"
string s3=s.substr(6);//s3="world"
string s4=s.substr(6,11);//s4="world"
string s5=s.substr(12);//out_of_range異常

更改string

  • string的insert()erase()可以接受迭代器,也可以接受下標;
s.insert(s.size(),5,'!');//在s.size()對應的位置(尾後)之前插入5個‘!’,也就是在s之後插入5個!
s.insert(0,s2);//在s的0位置之前插入s2的拷貝
s.insert(0,s2,0,s2.size());//在s的0位置之前插入 s2從0位置開始的s2.size()個字符
s.erase(s.size()-5,5); //刪除s的後5個字符
  • 與字符串數組的轉換:
const char *cp="Stately,plump Buck";
s.assign(cp,7);//使用cp的前7個字符來替換s s="Stately"
s.insert(s.size(),cp+7);//在s尾部添加從cp+7開始的字符 s="Stately,plump Buck"
  • append()replace()
string s1("abcd"),s2=s1;
s1.insert(s1.size(),"efg"); //s1="abcdefg"
s2.append("efg");  //s2="abcdefg"
s1.replace(4,3,"EFG");//s1="abcdEFG"
s2.replace(4,3,"EFGH");//s2="abcdEFGH"

搜索操作

  • s.find(args)在s中尋找args第一次出現的位置
  • s.rfind(args)在s中尋找args最後一次出現的位置
  • s.find_first_of(args)在s中尋找args中任意元素第一次出現的位置
  • s.find_first_not_of(args)在s中尋找args中任意元素第一次沒出現的位置
  • s.find_last_of(args)在s中尋找args中任意元素最後一次出現的位置
  • s.find_last_not_of(args)在s中尋找args中任意元素最後一次沒出現的位置
string s("Abcdefgsdf");
auto pos=s.find("Abcd");
auto pos1=s.rfind("f");
cout<<pos<<" "<<pos1<<endl;
string numbers("0123456789"),name("rbd35k4");
auto pos2=name.find_first_of(numbers);
auto pos3=name.find_last_of(numbers);
auto pos4=name.find_first_not_of(numbers);
auto pos5=name.find_last_not_of(numbers);
cout<<pos2<<" "<<pos3<<" "<<pos4<<" "<<pos5<<" "<<endl;

練習:查找string中的所有數字的位置

string temp("al46ien7g234lhbo4534ohln65oh4oh7j653dhg");
string::size_type pos=0;
while(temp.find_first_of(numbers,pos)!=string::npos){
    pos=temp.find_first_of(numbers,pos);
    cout<<"position:"<<pos<<endl;
    pos++;
}

compare函數:

  • 大於返回正數,小於返回負數,等於返回0
  • s.compare(s2)s與整個s2比較
  • s.compare(pos1,n1,s2)s從pos1開始的n1個字符與整個s2比較
  • s.compare(pos1,n1,s2,pos2,n2)s從pos1開始的n1個字符與s2從pos2開始的n2個字符比較
  • s.compare(cp)s與cp指向的字符數組比較
  • s.compare(pos1,n1,cp)s從pos1開始的n1個字符與cp指向的字符數組比較
  • s.compare(pos1,n1,cp,n2)s從pos1開始的n1個字符與cp指向的地址開始的n2個字符比較

數值轉換

  • 非成員函數
  • to_string(val):將任何算數類型數值val轉換成string
  • stoi(s):string轉換成int整型
  • stol(s):string轉換成long整型
  • stoul(s):string轉換成unsigned long整型
  • stoll(s):string轉換成long long整型
  • stoull(s):string轉換成unsigned long long整型
  • stof(s):string轉換成float浮點型
  • stod(s):string轉換成double浮點型
  • stold(s):string轉換成long double浮點型

練習:提取字符串中的數值

string a("pi=3.14");
double d=stod(a.substr(a.find_first_of("+-.0123456789")))
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章