C++之STL库:string类(用法列举和总结)

简介: C++之STL库:string类(用法列举和总结)

前言

       大家在学习STL库的时候一定要学会看英文文档,俗话说熟能生巧,所以还得多练!在使用string类之前,要包含头文件#include <string>和using namespace std;

文档链接:string - C++ Reference

一、string——构造相关操作

1. string(); (常用)

构造一个空字符串

string s1;

2. string(const char *s); (常用)

使用字符串构造一个实例化对象

string s2("hello world");

3. string(const string& str); (常用)

使用string的实例化对象去拷贝构造另外一个实例化对象

string s2("hello world");
string s3(s2);

4. string& operator=(const string& str); (常用)

string s2("hello world");
string s4 = s2;

5. string& operator=(const char *s); (常用)

string s5 = "hello world";

6. string& operator=(char c);

这里有一点需要注意,赋值运算符重载和拷贝构造的区别,两个对象都是定义之后的=是赋值运算符重载,如果是在定义的时候的=,属于拷贝构造;

所以代码1是不正确的:因为这是拷贝构造,而在拷贝构造这里,没有将一个字符拷贝给string类的函数实现

正确的写法:

    string s6;
    s6 = 'x';

7. string (const string& str, size_t pos, size_t len = npos);

拷贝str,在pos的下标开始,拷贝len个长度。

string s7 = "hello world";
string s8(s7, 0, 3);

8. string (const char* s, size_t n);

从s上拷贝前n个字符

string s9("hello world", 4);

9. string (size_t n, char c);

拷贝n个一样的字符

string s10(10, 'x');

10. string (InputIterator first, InputIterator last);

用迭代器拷贝,first为头,last为尾,拷贝first<= string <last,也就是拷贝从first位置开始,在last前一个位置停下来,不拷贝last位置;(默认迭代器的end是在\0位置)

    string s11 = "hello world";
    string s12(s11.begin(), s11.end()-1);

二、string——打印相关操作

可以从键盘上输入字符串,但是遇到空格就结束输入

    string s1;
    cin >> s1;
    cout << s1 << endl;

2. 输出字符串cout

将字符串的内容打印到显示器上

3. 获取字符串getline

从流中获取字符串,输入空格不会停止

    string s1;
    getline(cin, s1);
    cout << s1 << endl;

三、string——访问和遍历相关操作

1. char& operator[] (size_t pos);

通过下标访问字符串元素

    string s1 = "hello world";
    cout << s1[0] << endl;

2.  char& at (size_t pos);

跟下标访问一样,只是报错的方式不同;

    string s1 = "hello world";
    cout << "字符串第5个字符为:" << s1.at(4) << endl;

3. iterator begin();和 iterator end();

begin获取字符串第一个字符的迭代器(可以理解取第一个字符的地址)

end获取最后一个字符的下一个位置的迭代器(可以理解取最后一个有效字符下一个位置的地址)

    string s1 = "hello world";
    string::iterator begin = s1.begin(); 
    string::iterator end = s1.end();

可以像指针那样的遍历

    string s1 = "hello world";
    string::iterator begin = s1.begin();
    string::iterator end = s1.end();
    while(begin < end)
    {
        cout << *begin << ' ';
        ++begin;
    }
    cout << endl;

也可以访问第几个位置的元素,但是一般用下标访问了

    string s1 = "hello world";
    string::iterator begin = s1.begin();
    string::iterator end = s1.end();
    cout << "访问第1个元素为:" << *begin << endl;
    cout << "访问第5个元素为:" << *(begin + 4) << endl;
    cout << "访问倒数第3个元素为:" << *(end - 3) << endl;

4. reverse_iterator rbegin();和 reverse_iterator rend();

rbegin:指向的是最后一个有效字符的迭代器,从后往前访问;

rend:指向的是第一个字符之前的迭代器,从前往后访问;

    string s1 = "hello world";
    string::reverse_iterator  rb = s1.rbegin();
    string::reverse_iterator re = s1.rend();
    while(rb < re)
    {
        cout << *rb << ' ';
        ++rb;
    }

5. 范围for遍历

很简洁,但是底层依旧是迭代器;

    string s1 = "hello world";
    for(auto e : s1)
    {
        cout << e << ' ';
    }
    cout << endl;

四、string——容量相关操作

1. size

计算字符串的长度,以字节为单位

string s1 = "hello world";
int len = s1.size();
cout << len << endl;

2. capacity

       返回当前为字符串已经分配的空间大小,以字节为单位,这个空间表示出来的是给有效字符存的空间;


       但是实际的空间大小是比capacity表示出来的多一个空间,是存\0的;

分配的空间会大于字符串的size,但是分配的空间有可能不同,这取决于不同的编译器;

string s1 = "hello world";
cout << s1.capacity() << endl;

3. empty

判断字符串是否为空串:为空返回true(非0),不为空,返回false(0)

    string s1 = "hello world";
    cout << s1.empty() << endl;
    string s2;
    cout << s2.empty() << endl;

4. clear

只清空字符串的有效内容,不销毁空间;清空内容之后,size为0,capacity不变

    string s3 = "hello world";
    cout << "清空之前的容量:" << s3.capacity() << endl;
    cout << "清空之前的长度:" <<s3.size() << endl;
    s3.clear();
    cout << "清空之后的容量:" << s3.capacity() << endl;
    cout << "清空之后的长度:" <<s3.size() << endl;

5. reserve

为字符串保留空间


规则:

1. 扩容:当保留的空间 > capacity,认为扩容。每个编译器扩容的空间是不同的,总体上看就是:实际扩容下的空间≥要保留的空间;

2. 缩容:当保留的空间 < capacity ,认为缩容。有的编译器不会缩容,有的编译器会缩容,如果保留的空间<size,只缩容到size大小。

    string s4 = "hello world";
    cout << "扩容之前的容量:" << s4.capacity() << endl;
    s4.reserve(100);
    cout << "扩容之后的容量:" << s4.capacity() << endl;

    string s4 = "hello world";
    cout << "缩容之前的容量:" << s4.capacity() << endl;
    s4.reserve(2);
    cout << "缩容之后的容量:" << s4.capacity() << endl;

6. resize

void resize (size_t n);
void resize (size_t n, char c);

将有效字符的个数该成n个;

n > capacity :size = n ,capacity ≥ n,若提供c,则后面全为c字符,若没提供,则为‘\0’;

并不是\0就是无效字符,对于有效字符的设定是根据在0~size-1之间的都是有效字符;

    string s5 = "hello world";
    cout << s5 << endl;
    cout << "调整有效字符之前的容量:" << s5.capacity() << endl;
    cout << "调整有效字符之前的长度:" <<s5.size() << endl;
    s5.resize(30);
    cout << s5 << endl;
    cout << "调整有效字符之后的容量:" << s5.capacity() << endl;
    cout << "调整有效字符之后的长度:" <<s5.size() << endl;

    string s5 = "hello world";
    cout << s5 << endl;
    cout << "调整有效字符之前的容量:" << s5.capacity() << endl;
    cout << "调整有效字符之前的长度:" <<s5.size() << endl;
    s5.resize(30, 'x');
    cout << s5 << endl;
    cout << "调整有效字符之后的容量:" << s5.capacity() << endl;
    cout << "调整有效字符之后的长度:" <<s5.size() << endl;

size < n < capacity ,size = n,capacity不变;若提供c,则后面全为c字符,若没提供,则为‘\0’;

n < size ,size = n ,删数据,保留前n个有效字符,capacity不变;

五、string——增加操作

1. string& operator+= (char c);

尾插字符

    string s1 = "hello world";
    cout << s1 << endl;
    s1 += 'x';
    cout << s1 << endl;

2. string& operator+= (const string& str);

尾插字符串

    string s1 = "hello world";
    cout << s1 << endl;
    s1 += s1;
    cout << s1 << endl;

3. string& operator+= (const char* s);

尾插字符串

    string s1 = "hello world";
    cout << s1 << endl;
    s1 += " hello CSDN";
    cout << s1 << endl;

4. string& insert (size_t pos, const string& str);

在pos位置之前插入字符串

    string s1 = "hello world";
    string s2 = "CSDN";
    cout << "插入之前:" << s1 << endl;
    s1.insert(1, s2);
    cout << "插入之后:" << s1 << endl;

更多的应用:string::insert - C++ Reference

六、string——删除操作

1. string& erase (size_t pos = 0, size_t len = npos);

从pos位置删字符,删 len个字符;


npos代表-1,len为size_t类型,为无符号类型,就会被转换为整型的最大值,所以len为整型最大值,在不提供实参,就默认为npos;

    string s1 = "hello world";
    cout << "删除之前:" << s1 << endl;
    s1.erase(1,3);
    cout << "删除之后:" << s1 << endl;

2. iterator erase (iterator p);

删除迭代器所处的位置的字符

    string s1 = "hello world";
    cout << "删除之前:" << s1 << endl;
    s1.erase(s1.begin());
    cout << "删除之后:" << s1 << endl;

3.  iterator erase (iterator first, iterator last);

删除两个迭代器之间的字符,包括first,不包括last,[first, last)

    string s1 = "hello world";
    cout << "删除之前:" << s1 << endl;
    s1.erase(s1.begin(), s1.end()-1);
    cout << "删除之后:" << s1 << endl;

七、string——查找操作

在pos位置向后查找str这个string类的对象,返回找到的字符串的第一个位置的下标,没找到就返回npos
size_t find (const string& str, size_t pos = 0) const;
在pos位置向后查找s这个字符串,返回找到的字符串的第一个位置的下标,没找到就返回npos
size_t find (const char* s, size_t pos = 0) const;
在pos位置向后找,找s的前n个字符,找到返回第一个位置的下标,找不到返回npos
size_t find (const char* s, size_t pos, size_t n) const;
在pos位置向后查找字符c,返回找到的字符位置的下标,没找到就返回npos
size_t find (char c, size_t pos = 0) const;

显示详细信息

具体查看:hstring::find - C++ Reference

rfind就是从后往前找

八、string——构造子串

意思就是构造一个子串,从pos位置开始,往后len个字符都构造;

    string s2 = "hello world";
    size_t n = s2.find('w', 0);
    string s3 = s2.substr(n,s2.size() - n);
    cout << s2 << endl;
    cout << s3 << endl;

九、string——比较操作

大家试着去官网去查查,一定要熟悉英文文献哦~

relational operators (string) - C++ Reference

十、好用的函数

1. 字符串转换整数

to_string - C++ Reference

2. 整数转换字符串

<string> - C++ Reference

十一、不同的编码要使用对应的string

UTF-8 string
UTF-16 u16string
UTF-32 u32string
宽字符(双字节字符 wstring


相关文章
|
4天前
|
编译器 C++
【C++】string类的使用④(字符串操作String operations )
这篇博客探讨了C++ STL中`std::string`的几个关键操作,如`c_str()`和`data()`,它们分别返回指向字符串的const char*指针,前者保证以&#39;\0&#39;结尾,后者不保证。`get_allocator()`返回内存分配器,通常不直接使用。`copy()`函数用于将字符串部分复制到字符数组,不添加&#39;\0&#39;。`find()`和`rfind()`用于向前和向后搜索子串或字符。`npos`是string类中的一个常量,表示找不到匹配项时的返回值。博客通过实例展示了这些函数的用法。
|
4天前
|
存储 C++
【C++】string类的使用③(非成员函数重载Non-member function overloads)
这篇文章探讨了C++中`std::string`的`replace`和`swap`函数以及非成员函数重载。`replace`提供了多种方式替换字符串中的部分内容,包括使用字符串、子串、字符、字符数组和填充字符。`swap`函数用于交换两个`string`对象的内容,成员函数版本效率更高。非成员函数重载包括`operator+`实现字符串连接,关系运算符(如`==`, `&lt;`等)用于比较字符串,以及`swap`非成员函数。此外,还介绍了`getline`函数,用于按指定分隔符从输入流中读取字符串。文章强调了非成员函数在特定情况下的作用,并给出了多个示例代码。
|
4天前
|
C++
【C++】string类的使用④(常量成员Member constants)
C++ `std::string` 的 `find_first_of`, `find_last_of`, `find_first_not_of`, `find_last_not_of` 函数分别用于从不同方向查找目标字符或子串。它们都返回匹配位置,未找到则返回 `npos`。`substr` 用于提取子字符串,`compare` 则提供更灵活的字符串比较。`npos` 是一个表示最大值的常量,用于标记未找到匹配的情况。示例代码展示了这些函数的实际应用,如替换元音、分割路径、查找非字母字符等。
|
4天前
|
C++
C++】string类的使用③(修改器Modifiers)
这篇博客探讨了C++ STL中`string`类的修改器和非成员函数重载。文章介绍了`operator+=`用于在字符串末尾追加内容,并展示了不同重载形式。`append`函数提供了更多追加选项,包括子串、字符数组、单个字符等。`push_back`和`pop_back`分别用于在末尾添加和移除一个字符。`assign`用于替换字符串内容,而`insert`允许在任意位置插入字符串或字符。最后,`erase`函数用于删除字符串中的部分内容。每个函数都配以代码示例和说明。
|
4天前
|
安全 编译器 C++
【C++】string类的使用②(元素获取Element access)
```markdown 探索C++ `string`方法:`clear()`保持容量不变使字符串变空;`empty()`检查长度是否为0;C++11的`shrink_to_fit()`尝试减少容量。`operator[]`和`at()`安全访问元素,越界时`at()`抛异常。`back()`和`front()`分别访问首尾元素。了解这些,轻松操作字符串!💡 ```
|
4天前
|
存储 编译器 Linux
【C++】string类的使用②(容量接口Capacity )
这篇博客探讨了C++ STL中string的容量接口和元素访问方法。`size()`和`length()`函数等价,返回字符串的长度;`capacity()`提供已分配的字节数,可能大于长度;`max_size()`给出理论最大长度;`reserve()`预分配空间,不改变内容;`resize()`改变字符串长度,可指定填充字符。这些接口用于优化内存管理和适应字符串操作需求。
|
4天前
|
C++ 容器
【C++】string类的使用①(迭代器接口begin,end,rbegin和rend)
迭代器接口是获取容器元素指针的成员函数。`begin()`返回首元素的正向迭代器,`end()`返回末元素之后的位置。`rbegin()`和`rend()`提供反向迭代器,分别指向尾元素和首元素之前。C++11增加了const版本以供只读访问。示例代码展示了如何使用这些迭代器遍历字符串。
|
9天前
|
C++
【C++】日期类Date(详解)②
- `-=`通过复用`+=`实现,`Date operator-(int day)`则通过创建副本并调用`-=`。 - 前置`++`和后置`++`同样使用重载,类似地,前置`--`和后置`--`也复用了`+=`和`-=1`。 - 比较运算符重载如`&gt;`, `==`, `&lt;`, `&lt;=`, `!=`,通常只需实现两个,其他可通过复合逻辑得出。 - `Date`减`Date`返回天数,通过迭代较小日期直到与较大日期相等,记录步数和符号。 ``` 这是236个字符的摘要,符合240字符以内的要求,涵盖了日期类中运算符重载的主要实现。
|
4天前
|
存储 编译器 C语言
【C++】string类的使用①(默认成员函数
本文介绍了C++ STL中的`string`类,它是用于方便地操作和管理字符串的类,替代了C语言中不便的字符数组操作。`string`基于`basic_string`模板,提供类似容器的接口,但针对字符串特性进行了优化。学习资源推荐[cplusplus.com](https://cplusplus.com/)。`string`类提供了多种构造函数,如无参构造、拷贝构造、字符填充构造等,以及析构函数和赋值运算符重载。示例代码展示了不同构造函数和赋值运算符的用法。
|
4天前
|
编译器 C++
【C++】类和对象⑤(static成员 | 友元 | 内部类 | 匿名对象)
📚 C++ 知识点概览:探索类的`static`成员、友元及应用🔍。