【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;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值