C++STL之string

1.string对象的创建

        通过字符串来创建并初始化一个string对象,也可不传字符串直接创建,同时其支持拷贝构造和赋值操作,也就是说可以使用其它string对象来初始化或赋值string对象

int main()
{
   
    string s2;
    string s1 = "Hello World";
    string s3 = s1;
    
    s2 = s3;

    
    return 0;
}

2.string对象的遍历

         从string对象起,我们将开始了解一种不同于下标的新的遍历方式——迭代器遍历法,通过使用迭代器来遍历对象

int main()
{
   
    
    string s3 = "Hello World";
        

    
    //使用类对象.begin()获取其开始的迭代器
    string::iterator it = s3.begin();

    //类对象.end()为结束的迭代器
    while (it != s3.end())
    {
        cout << *it <<' ';


        //it++直到遇到结束迭代器
        it++;
    }
    cout << endl;





    return 0;
}

        迭代器的遍历看起来较为复杂,但其也可以用于其它类型上,如链表,它无法通过下标进行遍历,这种为不同类型的类似行为提供统一方案的规范实际上也是封装的一种体现

        当然,这里还有一种更简单的遍历方法,即范围for,其底层也是使用的迭代器

int main()
{
   
    
    string s3 = "Hello World";
    for (auto a : s3)
    {
        cout << a;
    }
    cout << endl;
    

    return 0;
}

        auto为自动类型,编译器会为a变量匹配合适的类型,换成char也可以,通过范围for,将s3中的每个元素赋值给a并进行遍历,当然,如果想对s3中的元素进行改变,使用auto&或char&即可

        除了以上两种遍历方法外,string是可以支持下标遍历的,其内部对[]运算符进行了重载,可以通过下标访问其内部字符串的成员

int main()
{
   
    
    string s3 = "Hello World";
    //通过s3.size()获取s3的元素个数(不含"\0")
    for (int i = 0; i < s3.size(); i++)
    {
        cout << s3[i];

    }
    cout << endl;
    


    return 0;
}

3.为string对象增添元素

        向string尾部添加字符或字符串,最简单方式就是使用+=

int main()
{
   
    
    string s3 = "Hello World";
    s3 += "Hello Hope";
    s3 += '2';

    //也可直接加等一个string对象
    string s1 = " Hello Dream";
    s3 += s1;

    cout << s3 << endl;


    return 0;
}

        除了+=外,也可以使用append(加字符串),push_back(加字符)

int main()
{
   
    
    string s3 = "Hello World";
    s3.append("Hello Hope");
    s3.push_back('w');
   
    cout << s3 << endl;


    return 0;
}

        使用insert向指定下标处插入字符串

int main()
{
   
    
    string s3 = "Hello World";
     //向指定位置插入字符串--对象.index(下标,字符串)
    s3.insert(5, " The");


    //向指定位置插入n个字符--对象.insert(下标,个数,字符)
    s3.insert(5, 4, 'c');
   
    cout << s3 << endl;
    


    return 0;
}

4.为string对象删除元素

        使用erase来删除指定位置指定个数的元素

int main()
{
   
    
    string s3 = "Hello World";
    //对象.erase(下标,要删除的元素个数)
    s3.erase(0, 5);

    cout << s3 << endl;



    return 0;
}

5.关于string的一些其它常用方法

        使用size获取string对象字符串长度(不包含“\0”),使用resize重新指定字符串长度并用指定字符填充,如果小于字符串长度就把多余的给截掉

int main()
{
   
    
    string s3 = "Hello World";
    //s3.resize(指定长度,指定填充字符)
    s3.resize(s3.size()+10,'i');

    cout << s3 << endl;    


    return 0;
}

        使用reverse重新设定字符串容量,如果设定的容量小于现有字符串长度,则保持原有容量不做修改,如果大于现有字符串长度而小于现有容量,则是否缩容由编译器决定,如果大于现有容量则进行扩容操作,直到达到或超过目标容量,使用capacity来查看对象当前容量

int main()
{
   
    
    string s3 = "Hello World";
    cout << "现有容量:" << s3.capacity() << endl;
    cout << "现有字符串长度:" << s3.size() << endl;

    s3.reserve(20);
    cout << "改变容量为20,大于现有:" <<s3.capacity() << endl;


    s3.reserve(15);
    cout << "改变容量为15,小于现有大于字符串长度:" << s3.capacity() << endl;


    s3.reserve(5);
    cout<<"改变容量为5,小于字符串长度:" << s3.capacity() << endl; 

    


    return 0;
}

        使用swap将该string对象和指定string对象内容交换

int main()
{
   
    
    string s3 = "Hello World";
   
    string s2 = "Hello Hope";

    s3.swap(s2);


    cout << "s3:" << s3 << endl;
    cout << "s2:" << s2 << endl;

    cout << s3 << endl;    

    

    
   





    return 0;
}

        使用find和rfind来正向和逆向查找指定对象并返回目标首次出现的下标,该对象可以是字符或字符串或string对象

int main()
{
   
    
    string s3 = "Hello World";
    //s3.find(目标对象,从哪个下标开始寻找)
    int rt = s3.find("Hello",0);
    int rt2 = s3.rfind("Hello",s3.size()-1);
    cout << "rt:" << rt << endl;
    cout << "rt2:" << rt2 << endl;                              

    cout << s3 << endl;    

    

    return 0;
}

        使用substr获取string对象的子串

int main()
{
   
    
    string s3 = "Hello World";
    //s3.substr(a,b)
    //从a下标开始截取b个字符
    string s2 = s3.substr(0, 5);

    cout << "s2:" << s2 << endl;                  


    return 0;
}

  • 11
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值