(c++)string类申明,函数,应用代码与详细注释

#include <iostream>

#include <string>

using namespace std;

void test1() //size, capacity, rserve, clear
{
    string s1("Hello World");
    cout << "size : " << s1.size() << endl; //s1长度
    cout << "capacity = " << s1.capacity() << endl; //s1容量
    s1.reserve(100); //预留空间;如果数字大于capacity,则capacity为数字,小于等于不变
    cout << "reserve capacity = " << s1.capacity() << endl; //预留后s1容量
    s1.clear(); //清除s1,即size = 0,但是在vs2022中capacity不会变
    cout << "clear size : " << s1.size() << endl; //s1长度
    cout << "clear capacity = " << s1.capacity() << endl; //s1容量
    cout << "clear s1 : " << s1 << endl;
}

void test2() //[],遍历,迭代器(iterator)
{
    string s1("Hello World");
    for (int i = 0; i < s1.size(); i++)
    {
        cout << s1[i] << " "; //[]是运算符重载,可以查看和修改s1[i]位置的值
    }
    cout << endl << endl;
    //普通的迭代器
    string::iterator it = s1.begin(); //迭代器,s1.begin()可以抽象为第一个位置的指针,但其实本质并不是指针
    while (it != s1.end()) //s1.end()是最后一个字符的下一个位置
    {
        //解引用it相当于拿到了s1的修改权,所以可以通过解引用it修改s1
        cout << *(it++) << " ";
    }
    cout << endl << endl;
    //反向迭代器
    string::reverse_iterator rit = s1.rbegin(); //反迭代器
    while (rit != s1.rend()) //对应使用rend
    {
        //反向也是++访问一下个字符
        cout << *(rit++) << " "; //打印出来是反向的s1
    }
    cout << endl << endl;
    //const迭代器
    string::const_iterator cit = s1.begin(); //const迭代器
    while (cit != s1.end()) //函数重载,编译器自动识别const类型
    {
        //因为是const,所以解引用不可以修改s1
        cout << *(cit++) << " ";
    }
    cout << endl << endl;
    //除此之外还有一个cosnt反向迭代器,即const + reverse
    //范围for的遍历,本质是const迭代器写法,不能修改只能查看(底层就是const迭代器)
    for (auto s : s1)
    {
        cout << s << " ";
    }
    cout << endl;
}

void test3() //增删改: append, push_back, pop_back, erase, +=, insert, swap, replace
{
    string s1("Hello World");
    //增:
    //push_back: 尾部增加一个字符
    string s2(s1); //拷贝构造
    s2.push_back(' ');
    s2.push_back('Y');
    s2.push_back('M');
    cout << "s2 : " << s2;
    cout << endl << endl;
    //append: 在末尾添加字符或者字符串
    string s3(s1);
    s3.append(" "); //添加一个字符为空的字符串(只能使用"")
    s3.append("YM"); //添加一个字符为YM的字符串
    cout << "s3 : " << s3 << endl;
    s3 = s1; //赋值运算符重载
    s3.append(5, 'y'); //添加5个字符y(只能使用'')
    cout << "s3 : " << s3 << endl;
    s3 = s1;
    s3.append("hello YMM", 5, 3); //在末尾添加字串(从子串下标5的位置开始,长度为3,长度是缺省值,不写默认到子串末尾)
    cout << "s3 : " << s3 << endl;
    cout << endl;
    //insert: 插入字符或字符串
    string s4(s1);
    s4.insert(5, " YM"); //在s4下标为5的位置插入" YM"
    cout << "s4 : " << s4 << endl;
    s4 = s1;
    s4.insert(5, " YMM Hello", 3); //在s4下标为5的位置插入子串前3个字符
    cout << "s4 : " << s4 << endl;
    s4 = s1;
    s4.insert(5, "Hello YMM", 5, 3); //在s4下标为5的位置插入子串(从子串下标5的位置开始,长度为3,长度是缺省值,不写默认到子串末尾)
    cout << "s4 : " << s4 << endl;
    s4 = s1;
    s4.insert(5, 3, '*'); //在s4下标为5的位置插入3个'*'
    cout << "s4 : " << s4 << endl;
    cout << endl;
    //+=: 用的最多的赋值运算符重载
    string s5(s1);
    string _ss5(" YM");
    s5 += _ss5; //可以直接加string类型的字符串或字符
    cout << "s5 : " << s5 << endl;
    s5 = s1;
    char _s5[] = " YM";
    s5 += _s5;
    cout << "s5 : " << s5 << endl; //也可以直接加char类型的字符串或者字符
    cout << endl;
    //删:
    //pop_back(): 直接尾删一个字符
    string s6("Hello World YMM");
    s6.pop_back();
    cout << "s6 : " << s6 << endl;
    //erase: 从字符串中删除一个或多个字符
    string s7 = "Hello YMM World";
    s7.erase(8, 1); //删除字符串(从字符串下标8的位置开始,长度为1,长度是缺省值,不写默认到字串末尾)
    cout << "s7 : " << s7 << endl;
    s7 = "YHello World";
    s7.erase(s7.begin()); //删除开头字符,同理s7.end() - 1删除结尾字符(因为end指向结尾的下一个位置)
    cout << "s7 : " << s7 << endl;
    cout << endl;
    //改:
    //replace: 替换
    string s8("Hello ** World");
    s8.replace(6, 2, "YM"); //将s8下标为6之后的2个数据替换为YM(如果为(6, 4, "YM"),则将** W替换为YM)
    cout << "s8 : " << s8 << endl;
    cout << endl;
    //swap: 交换
    string s9("Hello");
    string s10("World");
    swap(s9, s10); //算法里面的swap
    cout << "s9 : " << s9 << endl;
    s9.swap(s10); //string类里面的swap,更快
    cout << "s9 : " << s9 << endl << endl;
}

void test4() //查找复制: find, rfind, find_first(_not)_of, find_last(_not)_of, substr
{
    //查找: 所有的查找在无法找到要求字符时都会返回字符串长度(string::npos)
    string s1("Hello World");
    //find: 正向查找字符
    size_t pos = s1.find('l');
    cout << "pos(l) : " << pos << endl;
    //rfind: 反向查找字符
    size_t rpos = s1.rfind('l');
    cout << "rpos(l) : " << rpos << endl;
    // find_first_of: 查找字符串里给定子串中的字符
    size_t fpos = s1.find_first_of("ol");
    cout << "fpos : " << fpos << endl;
    // find_first_not_of: 查找字符串里非给定子串中的字符
    size_t nfpos = s1.find_first_not_of("ol");
    cout << "nfpos : " << nfpos << endl;
    //last即字符串反向查找,与上面类似
    size_t lpos = s1.find_last_of("ol");
    cout << "lpos : " << lpos << endl;
    cout << endl;
    //substr: 复制
    s1 = "Yanmuuu@outlook.com";
    pos = s1.find('@');
    string s = s1.substr(pos); //复制下标为pos之后的字符串到s中,也可以(pos, n)表示复制pos后n个字符,n为长度,默认缺省为字符串总长度
    cout << "substr s(@) : " << s << endl;
    cout << endl;
}

//字符串比较使用compare(str)或者运算符重载<, >, ==

int main()
{
    test1();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值