C++ STL 学习笔记 string

string
字符串
定义和初始化

string s1;
string s2(str);            //使用构造函数初始化
string s3(n,'c');          //s3是有n个连续的c字符组成的字符串

string对象上的操作

getline(is,s)            //从输入流中读取一行字符串赋给s
s.empty()
s.append(str)            //在s末尾加一个字符串
s.size()
s[i]
s1==s2                  //如果s1和s2的所含的字符完全一样,则相等
s.length();
s.erase(string::iterator it)  //删除it处的字符
s.erase(str.begin(),str.end())  //删除该范围内的字符
reverse(str.begin(),str.end())  //将该范围内的字符反转
transform(str.begin(),str.end(),str.begin(),toupper)//将begin到end出的字符转换成大写字母,并从beign出写入,小写是tolower

学习代码

  • c和c++风格的字符串
  • #include <iostream>
    #include <string>
    #pragma warning(disable:4996)
    
    //strcpy不安全被禁用,要想使用采用以下办法和加上 上面的注释信息
    /*其实在下面的输出错误信息中有解决方法,“To disable deprecation , use _CRT_SECURE_NO_WARNINGS”,
    意思是我们可以不进行兼容性检查,
    我们可以在项目-属性-配置属性-c/c++-预处理器-预处理定义里边加上一句:
    _CRT_SECURE_NO_WARNINGS */
    int main()
    {
        using namespace std;
        char pszName[20] = "张飞";  //c风格的字符串
        cout << pszName << endl;
    
        char *pszName2 = "张飞";
        cout << pszName2 << endl;
    
        string strName2("刘备"); 
        cout << strName2 << endl;
    
        string strName = "张飞";    //运算符重载
        cout << strName << endl;
    
        const char* pszConstString = "Hello String!"; 
        cout << pszConstString << endl;
    
        string  strFormatConst(pszConstString);  //使用string的构造函数
        cout << strFormatConst << endl;
    
        string  strCopy(pszConstString, 5);   //复制前5个字符
        cout << strCopy << endl;
    
        //c风格字符串的复制
        const char* constStr = "Hello C!";
        char  *pszCopy = new char[strlen(constStr) + 1];
        strcpy(pszCopy, constStr);
        cout << pszCopy << endl;
        delete[] pszCopy;
        getchar();
        return 0;
    }

  • 访问字符串中的元素以及字符串的连接
  • #include <iostream>
    #include <string>
    
    using namespace std;
    int main()
    {
        string strStlString("hello word");
        cout << "使用传统的方法显示字符串中的每一个字符" << endl;
        for (size_t i = 0;i< strStlString.length(); ++i)
        {
            cout << strStlString[i] << endl;
        }
        cout << endl;
    
        cout << "使用STL里的迭代器操作字符串中的字符" << endl;
        string str1 = "ni  hao";
        string str2 = "this is fun!";
        string::iterator i;
        for (i = strStlString.begin();i!=strStlString.end();++i)
        {
            cout << *i << endl;
        }
    
        //字符串的连接
        str1 += str2;
        cout << str1 << endl;
        str1.append(str2);
        cout << str1 << endl;
    
        getchar();
        return 0;
    }
    

  • 字符串的查找
  • 
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
    
        string strSample = "Hello World! Today is a good day";
        //字符串的查找
        size_t positon = strSample.find("ll", 0);
    
        if (positon != string::npos)
        {
            cout << "找到了!" << endl;
            cout << positon << endl;
        }
        else
        {
            cout << "没有找到哦" << endl;
        }
    
        //查找多个相同的子串
        size_t position1 = strSample.find("day", 0);
        while (position1 != string::npos)
        {
            cout << "在下标" << position1 << "出找到子串day" << endl;
            size_t position2 = position1 + 1;
            position1 = strSample.find("day", position2);
    
        }
    
        //查找多个相同的字符
        size_t position3 = strSample.find('o', 0);
        while (position3 != string::npos)
        {
            cout << "在下标" << position3 << "处找到o" << endl;
            size_t position4 = position3 + 1;
            position3 = strSample.find('o', position4);
        }
        getchar();
        return 0;
    
    }
    

  • 字符串中元素的删除及反转
  • 
    #include <iostream>
    #include <string>
    #include <algorithm>
    int main()
    {
        using namespace std;
        string strSample = "Hello String ! Wake up to a beautiful day";
        cout << strSample << endl;
    
        strSample.erase(13, 28);
        cout << strSample << endl;
    
        //find算法,迭代器
        string::iterator iChars = find(strSample.begin(), strSample.end(),'S');
        if (iChars != strSample.end())
        {
            strSample.erase(iChars);
        }
        cout << strSample << endl;
    
        strSample.erase(strSample.begin(), strSample.end());
        if (strSample.length()==0)
            cout << "this is a empty string" << endl;
    
    
        //字符串反转算法
        string strSample2 = "Hello String! We will reverse you!";
        cout << strSample2 << endl;
    
        reverse(strSample2.begin(), strSample2.end());
        cout << strSample2 << endl;
    
        //
        cout << "请输入一行字符串" << endl;
        string strInput;
        getline(cin, strInput);
        transform(strInput.begin(), strInput.end(),strInput.begin(),toupper);//从begin到end转换,转换后又存到以beign开始的地方
        cout << strInput << endl;
    
        transform(strInput.begin(), strInput.end(), strInput.begin(), tolower);
        cout << strInput << endl;
    
        getchar();
        return 0;
    }
    
    
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值