顺序容器--字符串操作


  • 对于字符串string的额外操作

    const char *cp = "Hello world!!!!";
    char noNuLL[] = { 'H', 'i' };
    string s1(cp);
    string s2(noNuLL, 2);
    string s3(noNuLL);//错误字符串需要以/0为结束符
    string s4(cp + 6, 5);
    string s5(s1, 6, 5);
    string p("Helo World!!!");
    string p2 = p.substr(0, 5);
    string p3 = p.substr(5);
    string p4 = p.substr(6, 11);
    cout << p << endl;
    cout << p2 << endl;
    cout << p3 << endl;
    cout << p4 << endl;
    
    字符串操作含义
    s.insert(pos,args)pos可以是下标或者迭代器,或者说所有线性存储容器都支持下标设置
    s.erase(pos,len)删除pos位置上长度为len个字符
    s.append(args)在s末尾添加args然后返回一个s的引用
    s.assign(args)这是一个替换函数。用args替换s中的字符串
    s.replace(range,args)删除s中范围range内的字符并且用args替换
#include<iostream>
#include<string>
#include <vector>
using namespace std;


void replace_string(string&s, const string &oldVal, const string &newVal)
{
    auto l = oldVal.size();
    if (!l)
        return;//表示要查找的字符串为空
    auto iter = s.begin();
    while (iter <= s.end() - 1)
    {
        auto iter1 = iter;
        auto iter2 = oldVal.begin();
        while (iter2 != oldVal.end() && *iter1 == *iter2)
        {
            iter1++;
            iter2++;
        }
        if (iter2 == oldVal.end())
        {
            iter = s.erase(iter, iter1);//由后至前逐个插入
            if (newVal.size())
            {
                iter2 = newVal.end();
                do
                {
                    iter2--;
                    iter2 = s.insert(iter, *iter2);
                } while (iter2 > newVal.begin());
            }
            iter += newVal.size();
        }
        else
            iter++;
    }


}




int main(int argc, char **argv)
{
    //////////////////////////////////////////////////////////////////////////
    //string操作
    const char *cp = "Hello world!!!!";
    char noNuLL[] = { 'H', 'i' };
    string s1(cp);
    string s2(noNuLL, 2);
    string s3(noNuLL);//错误字符串需要以/0为结束符
    string s4(cp + 6, 5);
    string s5(s1, 6, 5);
    string p("Helo World!!!");
    string p2 = p.substr(0, 5);
    string p3 = p.substr(5);
    string p4 = p.substr(6, 11);
    cout << p << endl;
    cout << p2 << endl;
    cout << p3 << endl;
    cout << p4 << endl;
    //string p5 = p.substr(16);//字符串数组越界,抛出异常
    //////////////////////////////////////////////////////////////////////////
    //9.41
    vector<char>vc{ 'g', 'f', 's', 'b', 'a' };
    string s(vc.data(), vc.size());
    cout << s<<endl;
    void input_string(string&s);



    system("pause");
}
//////////////////////////////////////////////////////////////////////////
//高效的处理动态增长的string
//在声明的过程中就直接定义了吗?应该是的,虽然定义与实现要分离但是这样会浪费大量的新建文件的时间,这时候小型文本编辑器就显得很重要了,而不会选择如此臃肿的vs2013
    void input_string(string&s)
    {
        s.reserve(100);
        char c;
        while (cin >> c)
            s.push_back(c);
    }

转载于:https://www.cnblogs.com/VCctor/p/5100692.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值