【C++標準模板庫STL學習筆記】STL string類

常用功能:【include<string>】

  • 複製
  • 連接
  • 查找字符和子字符串
  • 截短
  • 使用標準模板庫提供的算法實現字符串發轉和大小寫轉換

【如果編寫程序需要更好的支持非拉丁文,例如中文,則應當使用std::wstring,兩者使用同一模板類,故使用方法一致!】


複製

#include <iostream>
#include <string>

int main() {
    /* 初始化字符串 */
    const char* constCStyleString = "Hello String";
    std::string strFromConst(constCStyleString);

    std::cout << strFromConst << std::endl;

    /* 第一種拷貝方式 */
    std::string copy1 = strFromConst;
    std::cout << copy1 << std::endl;

    /* 第二種拷貝方式 */
    std::string copy2 (strFromConst);
    std::cout << copy2 << std::endl;
    
    /* 使用迭代器訪問字符內容 */
    std::string::const_iterator charLocator;
    for(auto charLocator = strFromConst.cbegin();
        charLocator != strFromConst.cend();
        ++charLocator){
        std::cout << *charLocator << std::endl;
    }
    /* 【此處應當注意,auto遵循的是c++11標準】 */

    return 0;
}

連接

#include <iostream>
#include <string>

int main() {
    std::string sampleString1("Hello");
    std::string sampleString2("String!");

    std::cout << sampleString1 + sampleString2 << std::endl;

    std::string sampleString3;
    sampleString3 = sampleString1.append(sampleString2);
    std::cout << sampleString3 << std::endl;

    return 0;
}

在string 中查找字符或子字符串

#include <iostream>
#include <string>

int main() {
    std::string sampleString("Hello String");
    size_t  pos;
    pos = sampleString.find('String');

    if(pos != std::string::npos){
        std::cout << pos << std::endl;
    } else{
        std::cout << "not found" << std::endl;
    }
    /* 11 */


    std::string sampleString2("Hello String");
    size_t pos2 = sampleString2.find_first_not_of('H');
    size_t pos3 = sampleString2.find_first_not_of('h');
    std::cout << pos2 << std::endl;  /* 1 */
    std::cout << pos3 << std::endl;  /* 0 */


    std::string sampleString3("Hello String");
    size_t pos4 = sampleString3.find_last_not_of('g');
    size_t pos5 = sampleString3.find_last_not_of('G');
    std::cout << pos4 << std::endl;  /* 10 */
    std::cout << pos5 << std::endl;  /* 11 */


    return 0;
}

截斷

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

int main() {
    std::string sampleStr("Hello String! Wake up to a beautiful day!");
//    sampleStr.erase(13, 28);
    /* 第一個參數:偏移長度; 第二個參數:刪除長度,默認則後面全部刪除 */

//    sampleStr.erase(sampleStr.begin(), sampleStr.end());
    std::string::iterator charLocator = find(sampleStr.begin(), sampleStr.end(), 'S');
    /* 若不存在該字符,則會清空字符串,所以最好增加判斷 */
    if(charLocator != sampleStr.end()){
        sampleStr.erase(charLocator);
    } else{
        std::cout << "not found" << std::endl;
    }

    std::cout << sampleStr << std::endl;


    return 0;
}

字符串反轉

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

int main() {
    std::string str("Hello String!");
    std::cout << str << std::endl;
//    Hello String!

    std::reverse(str.begin(), str.end());
    std::cout << str << std::endl;
    std::reverse(str.begin(), str.end());
//    !gnirtS olleH

    std::transform(str.begin(), str.end(), str.begin(), ::toupper);
    std::cout << str << std::endl;
//    HELLO STRING!

    std::transform(str.begin(), str.end(), str.begin(), ::tolower);
    std::cout << str << std::endl;
//    hello string!
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章