C++入门之stl六大组件--String库函数的介绍

文章目录

一、为什么学习string类

1.C语言中的字符串

二、标准库中的string类

1.string类

2.string类的常用接口

1.string类对象的常见构造

2.string类对象的容量操作

3.string类对象的访问以及遍历操作

4.string类对象的修改操作

5.string类的非成员函数

总结

 


一、为什么学习string类

1.C语言中的字符串

C语言中,字符串是以\0结尾的一些字符集合,为了操作方便,C库中提供了一些str系列的库函数,但是这些库函数是与字符串分离开的,不太符合oop的思想,而且底层空间需要用户自己管理,如果不注意可能会访问越界。

二、标准库中的string类

1.string类

在使用string类的时候,必须包含#include头文件以及using namespace std;

1.string类是表示字符串的字符串类

2.该类的接口与常规容器的接口基本相同,再添加了一些专门用来操作string的常规操作

3.string再底层实际是:basic_string模板类的别名

4.不能操作多字节或者变长字符的序列

2.string类的常用接口

1.string类对象的常见构造

函数名称 功能说明
string() 无参构造,构造空的string类对象,即空字符串
string(const char *s) 用c-string来构造string类对象
string(const string&s) 拷贝构造,传入一个string类
string(size_t n,char c) string类对象中包含n个字符c

使用方法:

string s1; //构造一个空的string类对象s1
string s2("hello string"); // 用c字符串构造string类对象s2
string s3(s2) //拷贝构造s3

2.string类对象的容量操作

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

使用方法:

void test()
{
    string s("hello cplusplus");
    cout<<s.size()<<endl;
    cout<<s.capacity<<endl;
    
    //将s中的字符串清空,清空只是将size清0,不改变底层空间的大小
    s.clear();
    
    //将s中的有效字符增加到20个,多出位置用'a'填充
    s.resize(20,'a');

    
    //将s中有效字符增加到15个,多出位置用缺省值\0填充
    
    s.resize(15);

    string s1;
    
    s1.reserve(100); 
}
  • 使用reserve的情况:可以预留n个空间,避免扩容带来的开销。当reserive参数小于string的底层空间总大小时,reserver不会改变容量的大小

3.string类对象的访问以及遍历操作

函数名称 功能说明
operator[] 返回pos位置的字符,const string类对象调用
begin+end begin获取一个 字符的迭代器+end获取最后一个字符的下一个位置的迭代器
rbegin+rend rbegin获取最后一个字符的下一个位置的迭代器+rend获取第一个字符的迭代器
范围for 一种语法糖,c++11中新的遍历方式

使用方法:

void test()
{
    string s("hello cplusplus");
    
    //1.for+operator[]
    for(int i = 0; i<size;++i)
    {
        cout<<s[i]<<endl;
    }

    //2.迭代器  暂时可以将迭代器看成一个指针
    string::iterator it = s.begin();
    while(it!=s.end())
    {
        cout<<*it<<endl;
        ++it;
    }

    //反向迭代器rbegin
    auto rit = s.rbegin();
    while(rit!+s.rend())
    {
        cout<<*rit<<endl;
        ++rit;
    }

    //3.范围for 底层也是用的迭代器
    for(auto ch:s)
    {
        cout<<ch<<end;
    }

4.string类对象的修改操作

函数名称 功能说明
push back 在字符串后面增加一个字符
append 在字符串后面增加一个字符串
operator+= 在字符串后面增加一个字符串str
c str 返回c格式的字符串
find+npos 从字符串pos位置开始往后找字符c,返回该字符在字符串中的位置。npos是string里的一个静态成员变量,默认值为-1
rfind 从字符串pos位置开始往前找字符c,返回该字符在字符串中的位置
substr 在str中从pos位置开始,截取n个字符,然后将其返回

使用方法:

void test()
{
    string str("hello");
    str.push_back(' '); //追加一个空格
    str.append("world");
    str +='b';
    str += 'it';


    //通过rfind获取file的后缀 从后往前找
    string file("test.cpp");
    size_t pos = file.rfind('.');
    string suffix(file.substr(pos,file.size()-pos)); //获取后缀给suffix 获取子串,从pos位置向后找,找size-pos个长度 
    
    
       //取出url的域名
      //协议+ 域名 + 资源
      string url("http://www.cpluplus.com/reference/string/string/find/");
      size_t start = url.find("://");
      if(start == npos)
        return ; //无效域名
      start+= 3;
      size_t finish = url.find('/',start);  
      string address = url.substr(start,finsh - start); //取出中间的子串

}
    

5.string类的非成员函数

函数 功能说明
operator+ 使用传值返回,深拷贝效率较低
operator>> 输入运算符重载
operator<< 输出运算符重载
getline 获取一行字符串
relational operators 大小比较

总结

本章主要介绍了string类以及string类的一些接口函数

猜你喜欢

转载自blog.csdn.net/jolly0514/article/details/131776208
今日推荐