【C++】string类的使用②(元素获取Element access)

简介: ```markdown探索C++ `string`方法:`clear()`保持容量不变使字符串变空;`empty()`检查长度是否为0;C++11的`shrink_to_fit()`尝试减少容量。`operator[]`和`at()`安全访问元素,越界时`at()`抛异常。`back()`和`front()`分别访问首尾元素。了解这些,轻松操作字符串!💡```

==clear==

在这里插入图片描述
void clear();
将string串中的内容都删除,使其变成空串(length变成0),但容量capacity不会改变

使用样例:

#include <iostream>
#include <string>
using namespace std;
int main()
{
   
   
    string str("hello world");
    cout << str << endl;
    cout << "str.size():" << str.size() << endl;
    cout << "str.capacity():" << str.capacity() << endl;

    str.clear();

    cout << str << endl;
    cout << "str.size():" << str.size() << endl;
    cout << "str.capacity():" << str.capacity() << endl;
    return 0;
}

在这里插入图片描述

==empty==

在这里插入图片描述
bool empty() const
返回值:string串的长度(length)是否为0,如果为零,返回真(true,1),如果不为零,返回假(false,0)

使用样例:

#include <iostream>
#include <string>
using namespace std;
int main()
{
   
   
    string str1("hello world");
    string str2;

    cout << "str1.length():" << str1.length() << endl;
    cout << "str2.length():" << str2.length() << endl;

    cout << endl;

    cout << "str1.empty():" << str1.empty() << endl;
    cout << "str2.empty():" << str2.empty() << endl;
    return 0;
}

在这里插入图片描述

==shrink_to_fit==

在这里插入图片描述
void shrink_to_fit();
C++11新增接口。
此接口函数的作用是缩容,但是其具体怎么实现,其实也是C++未定义的。其作用和reserve,n小于capacity时的情况差不多,不同编译器会有不同的解释和实现。

注:缩容对编译器来说开销一般都不小,所以非必要情况少使用缩容。

🔥元素获取(Element access)

在这里插入图片描述

==operator[ ]==

在这里插入图片描述
char& operator[] (size_t pos);
const char& operator[] (size_t pos) const;
学过类和对象操作符重载的都知道,这里是一个[ ]的操作符重载,可以通过 方括号[ ]+下标获取串中的元素的引用

注:同时重载了const版本的方括号[ ]访问,当string对象为const类型时,下标获取的元素只能读,不能改。

代码样例和at放一起了。

==at==

在这里插入图片描述
char& at (size_t pos);
const char& at (size_t pos) const;
at的功能和operator[ ]相同,都是通过下标访问串中的元素。当string对象为非const类型时,也可以使用此访问对串进行内容上的改动。

使用样例:

#include <iostream>
#include <string>
using namespace std;
int main()
{
   
   
    // operator[]
    string str("Test string");
    str[1] = 'T';
    for (int i = 0; i < str.length(); ++i)
    {
   
   
        cout << str[i] << " ";
    }
    cout << endl;

    // at
    str.at(2) = 'T';
    for (int i = 0; i < str.length(); ++i)
    {
   
   
        cout << str.at(i) << " ";
    }
    cout << endl;
    return 0;
}

在这里插入图片描述

注:at和operator[ ]也有区别,当下标pos越界时,使用at访问程序会抛异常,能被try...catch捕获;而用operator[ ]访问则会直接报错

==back和front==

C++11新增语法。
在这里插入图片描述
char& back();
const char& back() const;
返回string对象串的末尾元素的引用

非const类型的string对象可以通过此函数更改串元素内容。

在这里插入图片描述
char& front();
const char& front() const;
返回string对象串的首元素的引用

非const类型的string对象可以通过此函数更改串元素内容。

使用样例:

#include <iostream>
#include <string>
using namespace std;
int main()
{
   
   
    string str("test string");
    str.front() = 'T';
    str.back() = 'G';
    cout << str << endl;
    return 0;
}

在这里插入图片描述

结语

本篇博客,介绍了9个容量接口(Capacity),它们有查询string串长度和更改长度的(size,length,resize),也有查询容量和更改容量的(capacity,reserve),和清理的(clear)。同时也讲到了种访问string对象串中元素的四种方式(operator[ ],at,front和back)。以上所提到各种接口和方法能让我们更加方便的操控string对象中的容量和内容,在熟练它们之后,就可以尽量避免使用那烦人的静态字符数组了
博主会继续分享关于string类的使用以及STL更多的内容,感谢大家的支持。♥

相关文章
|
6天前
|
C++ 容器
C++中自定义结构体或类作为关联容器的键
C++中自定义结构体或类作为关联容器的键
13 0
|
6天前
|
存储 算法 搜索推荐
【C++】类的默认成员函数
【C++】类的默认成员函数
|
5天前
|
安全 编译器 C语言
【C++数据结构】string的模拟实现
【C++数据结构】string的模拟实现
|
5天前
|
存储 安全 编译器
【C++】类和对象(下)
【C++】类和对象(下)
【C++】类和对象(下)
|
4天前
|
编译器 C++
virtual类的使用方法问题之C++类中的非静态数据成员是进行内存对齐的如何解决
virtual类的使用方法问题之C++类中的非静态数据成员是进行内存对齐的如何解决
|
4天前
|
编译器 C++
virtual类的使用方法问题之静态和非静态函数成员在C++对象模型中存放如何解决
virtual类的使用方法问题之静态和非静态函数成员在C++对象模型中存放如何解决
|
4天前
|
编译器 C++
virtual类的使用方法问题之在C++中获取对象的vptr(虚拟表指针)如何解决
virtual类的使用方法问题之在C++中获取对象的vptr(虚拟表指针)如何解决
|
5天前
|
编译器 C++
【C++】类和对象(中)
【C++】类和对象(中)
|
5天前
|
存储 编译器 程序员
【C++】类和对象(上)
【C++】类和对象(上)
|
6天前
|
存储 编译器 C++
【C++】类和对象(下)
【C++】类和对象(下)