彼岸花开C++,string常用接口


欢迎访问小马的博客,如果觉得小马的博客有帮助的话,记得点赞收藏加关注哦~~~   


标准库中的string类

string类的常用接口说明

(1)string类对象的常见构造

(constructor)函数名称                 功能说明                   
string()构造空的string类对象,即空字符串
string(const char* s)用C-string来构造string类对象
string(size_t n,char c)string类对象中包含n个字符c
string(const string&s)拷贝构造函数
void test_string()
{
    string s1;                    // 构造空的string类对象
    string s2("Hello world!");    // 用C格式字符串构造string类对象s2
    string s3(s2);                // 拷贝构造s3
}

(2)string类对象的容量操作

函数名称             功能说明
size返回字符串的有效字符长度
length返回字符串的有效字符长度
capacity返回空间总大小
empty检测字符串释放为空串,是返回true,否则返回false
clear清空有效字符
reverse为字符串预留空间
resize将有效字符的个数改成n个多出的空间用字符c填充

// 测试string容量相关的接口
// size/clear/resize
void Teststring1()
{
    // 注意:string类对象支持直接用cin和cout进行输入和输出
    string s("hello, world!!!");
    cout << s.size() << endl;
    cout << s.length() << endl;
    cout << s.capacity() << endl;
    cout << s << endl;

    // 将s中的字符串清空,注意清空时只是将size清0,不改变capacity的大小
    s.clear();
    cout << s.size() << endl;
    cout << s.capacity() << endl;

    // 将s中有效字符个数增加到10个,多出位置用'n'进行填充
    // “aaaaaaaaaa”
    s.resize(10, 'n');
    cout << s.size() << endl;
    cout << s.capacity() << endl;

    // 将s中有效字符个数增加到15个,多出位置用缺省值'\0'进行填充
    // "aaaaaaaaaa\0\0\0\0\0"
    // 注意此时s中有效字符个数已经增加到15个
    s.resize(15);
    cout << s.size() << endl;
    cout << s.capacity() << endl;
    cout << s << endl;

    // 将s中有效字符个数缩小到5个
    s.resize(5);
    cout << s.size() << endl;
    cout << s.capacity() << endl;
    cout << s << endl;
}

void Teststring2()
{
    string s;
    // 测试reserve是否会改变string中有效元素个数
    s.reserve(100);
    cout << s.size() << endl;
    cout << s.capacity() << endl;

    // 测试reserve参数小于string的底层空间大小时,是否会将空间缩小
    s.reserve(50);
    cout << s.size() << endl;
    cout << s.capacity() << endl;
}

注意:

  1. size()与length()方法底层实现原理完全相同,引入size()的原因是为了与其他容器的接口保持一致,一般情况下基本都是用size()
  2. clear()只是将string中的有效字符清空,不会改变底层空间的大小
  3. resize(size_t n)与resize(size_t n,char c)都是将字符串中有效字符个数改变到n个,不同的是当字符个数增多时:resize(n)用0来填充多出的元素空间,resize(size_t n ,char c)用字符c填充多出的元素空间。(resize在改变元素个数时,如果是元素个数增多,可能会改变底层容量的大小,如果是将元素个数减少,底层空间总大小不变。
  4. reverse(size_t res_arg = 0):为string预留空间,不改变有效元素个数,当reverse的参数小于string的底层空间大小时,reserve不会改变容量大小。

(3)string类对象的访问操作及遍历操作

函数名称功能说明
operator[ ]返回pos位置的字符,const string类的对象的调用
begin+endbegin获取一个字符的迭代器+end获取最后一个字符下一个位置的迭代器
rbegin+rendbegin获取一个字符的迭代器+end获取最后一个字符下一个位置的迭代器
范围forC++11支持更简洁的范围for的新遍历方式
// string的遍历
// begin()+end()   for+[]  范围for
// 注意:string遍历时使用最多的还是for+下标 或者 范围for(C++11后才支持)
// begin()+end()大多数使用在需要使用STL提供的算法操作string时,比如:采用reverse逆置string
void Teststring3()
{
    string s1("hello world");
    const string s2("Hello world");
    cout << s1 << " " << s2 << endl;
    cout << s1[0] << " " << s2[0] << endl;

    s1[0] = 'H';
    cout << s1 << endl;

    // s2[0] = 'h';   代码编译失败,因为const类型对象不能修改
}

void Teststring4()
{
    string s("hello world");
    // 3种遍历方式:
    // 需要注意的以下三种方式除了遍历string对象,还可以遍历是修改string中的字符,
    // 另外以下三种方式对于string而言,第一种使用最多
    // 1. for+operator[]
    for (size_t i = 0; i < s.size(); ++i)
        cout << s[i];
    cout << endl;

    // 2.迭代器
    string::iterator it = s.begin();
    while (it != s.end())
    {
        cout << *it;
        ++it;
    }
    cout << endl;

    // string::reverse_iterator rit = s.rbegin();
    // C++11之后,直接使用auto定义迭代器,让编译器推到迭代器的类型
    auto rit = s.rbegin();
    while (rit != s.rend())
    {
        cout << *rit;
        ++rit;
    }
    cout << endl;

    // 3.范围for
    for (auto ch : s)
        cout << ch;
    cout << endl;
}


(4)string类对象的修改操作

函数名称功能说明
push_back在字符串后尾插字符c
append在字符串后追加一个字符串
operator+=在字符串后追加字符串str
c_str返回C格式字符串
find+npos从字符串pos位置开始向后找字符c,返回该字符在字符串中的位置
rfind从字符串pos位置开始向后找字符c,返回该字符在字符串中的位置
substr从str中从pos位置开始,截取n个字符,然后将其返回
// 测试string:
// 1. 插入(拼接)方式:push_back  append  operator+=
// 2. 正向和反向查找:find() + rfind()
// 3. 截取子串:substr()
// 4. 删除:erase
void Teststring5()
{
    string str;
    str.push_back(' ');   // 在str后插入空格
    str.append("hello");  // 在str后追加一个字符"hello"
    str += 'w';           // 在str后追加一个字符'b'
    str += "orid";          // 在str后追加一个字符串"it"
    cout << str << endl;
    cout << str.c_str() << endl;   // 以C语言的方式打印字符串

    // 获取file的后缀
    string file("string.cpp");
    size_t pos = file.rfind('.');
    string suffix(file.substr(pos, file.size() - pos));
    cout << suffix << endl;

    // npos是string里面的一个静态成员变量
    // static const size_t npos = -1;

    // 取出url中的域名
    string url("http://www.cplusplus.com/reference/string/string/find/");
    cout << url << endl;
    size_t start = url.find("://");
    if (start == string::npos)
    {
        cout << "invalid url" << endl;
        return;
    }
    start += 3;
    size_t finish = url.find('/', start);
    string address = url.substr(start, finish - start);
    cout << address << endl;

    // 删除url的协议前缀
    pos = url.find("://");
    url.erase(0, pos + 3);
    cout << url << endl;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值