前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【C++干货基地】揭秘C++STL库的魅力:stiring的初步了解和使用

【C++干货基地】揭秘C++STL库的魅力:stiring的初步了解和使用

作者头像
鸽芷咕
发布2024-05-26 16:49:08
690
发布2024-05-26 16:49:08
举报
文章被收录于专栏:C++干货基地C++干货基地

一、STL是什么?

STL我相信各位学C++的肯定都不会陌生,C++自从模版出来之后就发生了革命性的意义。有了模版这个东西我们就可以只书写一个库来不给不同类型的数据使用。

STL(standard template libaray-标准模板库):是C++标准库的重要组成部分,不仅是一个可复用的组件库,而且是一个包罗数据结构与算法的软件框架。

二、STL的六大组件

STL主要是由四大组件组成的,前面说了STL 是一个包罗数据结构与算法的软件框架 其中里面的容器就是数据结构库含有各种常用的数据结构

  • 例如 顺序表 链表 队列 二叉树 等等常用数据结构
  • 其中今天介绍的string 其实也算是 STL 的一员是 存放字符的顺序表

但是由于历史原因,string是先出来的 STL 是后面由惠普实验室后开发出来开源所以人们并没有把string 归类到STL 之中。

三、我们为什么要学string?

在C语言中,字符串是以’\0’结尾的一些字符的集合,为了操作方便,C标准库中提供了一些str系列的库函数。

  • 但是这些库函数与字符串是分离开的,不太符合OOP的思想.
  • 而且底层空间需要用户自己管理,稍不留神可能还会越界访问。

所以在C++中 专门把字符串操作封装成了 string 容器,来给开发者更好的调用接口支持。不用去管理底层的空间分配使得使用更加省心。

3.1 string 的定义

  1. string是表示字符串的字符串类
  2. 该类的接口与常规容器的接口基本相同,再添加了一些专门用来操作string的常规操作。 比特就业课
  3. string在底层实际是:basic_string模板类的别名,typedef basic_string<char, char_traits, allocator> string;
  4. 不能操作多字节或者变长字符的序列。

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

四、string的常用接口使用

4.1 成员函数

构造函数

构造函数介绍我们初始化string 对象的几种方法

  • 1. 构造空的string类对象,即空字符串
代码语言:javascript
复制
int main()
{
	string s1();
	return 0;
}
  • 2. 用C-string来构造string类对象
代码语言:javascript
复制
#include<iostream>
using namespace std;

int main()
{
	string s1("hello gugu");
	cout << s1 << endl;
	return 0;
}
  • 3.使用string 中的 pos 位置开始,n个字符开始构造
代码语言:javascript
复制
#include<iostream>
using namespace std;

int main()
{
	string s1("hello gugu");
	string s2(s1, 6, 4);
	cout << s2 << endl;
	return 0;
}
  • 4.使用 n 个字符初始化
代码语言:javascript
复制
#include<iostream>
using namespace std;

int main()
{
	string s1(4,'x');
	cout << s1 << endl;
	return 0;
}
拷贝构造
代码语言:javascript
复制
#include<iostream>
using namespace std;

int main()
{
	string s1("hello gugu");
	string s2(s1);
	cout << s2 << endl;
	return 0;
}
operator=
代码语言:javascript
复制
#include<iostream>
using namespace std;

int main()
{
	string s1("hello gugu");
	string s2= s1;
	cout << s2 << endl;
	return 0;
}

4.2 迭代器的使用

迭代器是C++提供的一种新的遍历方式,其底层是一种类似指针的实现方式。可能很多人觉得这有什么可说的,但是迭代器不仅可以遍历string还能遍历二叉树链表是一种通用的遍历方式。

string 的三种遍历方式
  • 使用迭代器遍历
代码语言:javascript
复制
#include<iostream>
using namespace std;

int main()
{
	string s1("hello gugu");
	
	//使用迭代器遍历
	string::iterator it1 = s1.begin();
	while (it1 != s1.end())
	{
		cout << *it1 << " ";
		it1++;
	}
	cout << endl;

	//使用迭代器修改
	string::iterator it2 = s1.begin();

	while (it2 != s1.end())
	{
		*it2 -= 1;
		cout << *it2 << " ";
		it2++;
	}
	cout << endl;

	return 0;
}
  • 使用方括号遍历 【】
代码语言:javascript
复制
#include<iostream>
using namespace std;

int main()
{
	string s1("hello gugu");
	
	for (int i = 0; i < s1.size(); i++)
	{
		cout << s1[i] << ' ';
	}
	cout << endl;

	return 0;
}
  • 使用范围 for 遍历
代码语言:javascript
复制
#include<iostream>
using namespace std;

int main()
{
	string s1("hello gugu");
	
	for (auto e : s1)
	{
		cout << e << ' ';
	}
	cout << endl;
	return 0;
}
rbegin && rend

这俩就是反向迭代器,使用他们打印出来的结果是从后往前

代码语言:javascript
复制
int main()
{
	string s1("hello gugu");
	
	//使用迭代器遍历
	string::reverse_iterator rit = s1.rbegin();
	while (rit != s1.rend())
	{
		cout << *rit << " ";
		++rit;
	}
	cout << endl;
	return 0;
}

4.3 容量部分

capacity 获取当前容量
代码语言:javascript
复制
#include<iostream>
using namespace std;

int main()
{
	string s1("hello gugu");
	
	cout << s1.capacity() << endl;
	return 0;
}
size 获取当前存储了多少字符
代码语言:javascript
复制
#include<iostream>
using namespace std;

int main()
{
	string s1("hello gugu");
	
	cout << s1.size() << endl;
	return 0;
}
resize 减少字符存储,或填充字符
代码语言:javascript
复制
#include<iostream>
using namespace std;

int main()
{
	string s1("hello gugu");
	s1.resize(3, 'x');
	cout << s1 << endl;
	s1.resize(5);
	cout << s1 << endl;
	return 0;
}
reserve 为string扩容
代码语言:javascript
复制
#include<iostream>
using namespace std;

int main()
{
	string s1("hello gugu");
	cout << s1.capacity() << endl;
	s1.reserve(6);
	cout << s1.capacity() << endl;
	s1.resize(100);
	cout << s1.capacity() << endl;
	return 0;
}
clear 清空所有字符
代码语言:javascript
复制
#include<iostream>
using namespace std;

int main()
{
	string s1("hello gugu");
	cout << s1 << endl;
	s1.clear();
	cout << s1 << endl;
	return 0;
}
empty 判断当前字符串是否为空
代码语言:javascript
复制
#include<iostream>
using namespace std;

int main()
{
	string s1("hello gugu");

	cout << s1.empty() << endl;
	return 0;
}
shrink_to_fit 为当前字符串请求缩容
代码语言:javascript
复制
#include<iostream>
using namespace std;

int main()
{
	string s1("hello gugu");
	cout << s1.capacity() << endl;
	s1.reserve(100);
	cout << s1.capacity() << endl;
	s1.shrink_to_fit();
	cout << s1.capacity() << endl;
	return 0;
}

4.4 元素访问

operator[]
代码语言:javascript
复制
```cpp
#include<iostream>
using namespace std;

int main()
{
	string s1("hello gugu");
	
	for (int i = 0; i < s1.size(); i++)
	{
		cout << s1[i] << ' ';
	}
	cout << endl;

	return 0;
}

4.5 修改

+= 操作
代码语言:javascript
复制
#include<iostream>
using namespace std;

int main()
{
	string s1("hello gugu");
	string s2("xxxxx");

	s1 += s2;
	cout << s1 << endl;
	s1 += "vvvv";
	cout << s1 << endl;
	s1 += 'x';
	cout << s1 << endl;
	return 0;
}
append 追加字符或字符串
代码语言:javascript
复制
#include<iostream>
using namespace std;

int main()
{
	string s1("hello gugu");
	string s2("xxxxx");

	s1.append(s2);
	cout << s1 << endl;
	s1.append("vvvv");
	cout << s1 << endl;
	s1.append(4,'c');
	cout << s1 << endl;
	s1.append("abcdef",3);
	cout << s1 << endl;

	return 0;
}
push_back 尾插
代码语言:javascript
复制
#include<iostream>
using namespace std;

int main()
{
	string s1("hello gugu");
	s1.push_back('x');
	cout << s1 << endl;

	return 0;
}
assign 替换字符串
代码语言:javascript
复制
#include<iostream>
using namespace std;

int main()
{
	string s1("hello gugu");
	string s2("xxxxxxx");
	s1.assign(s2);
	cout << s1 << endl;
	s1.assign("Linux C++");
	cout << s1 << endl;
	s1.assign(5,'c');
	cout << s1 << endl;
	return 0;
}
insert 插入
代码语言:javascript
复制
#include<iostream>
using namespace std;

int main()
{
	string s1("hello gugu");
	string s2("C++");
	s1.insert(6, s2);
	cout << s1 << endl;

	s1.insert(2, "xxxx");
	cout << s1 << endl;

	s1.insert(6, 2,'v');
	cout << s1 << endl;

	s1.insert(6,"bbbbbb",2);
	cout << s1 << endl;
	
	return 0;
}
erase 删除字符串的一部分,减少其长度
代码语言:javascript
复制
#include<iostream>
using namespace std;

int main()
{
	string s1("hello gugu");
	cout << s1 << endl;
	cout << s1.size() << endl;
	cout << s1.capacity() << endl;
 
	s1.erase(5);
	cout << s1 << endl;
	cout << s1.size() << endl;
	cout << s1.capacity() << endl;

	return 0;
}
replace 替换
代码语言:javascript
复制
#include<iostream>
using namespace std;

int main()
{
	string s1("hello gugu");
	
	s1.replace(5,1,"C++");
	cout << s1 << endl;
	

	return 0;
}
swap交换
代码语言:javascript
复制
#include<iostream>
using namespace std;

int main()
{
	string s1("hello gugu");
	string s2("C++ Linux");

	cout << s1 << endl;
	cout << s2 << endl;

	swap(s1, s2);
	cout << s1 << endl;
	cout << s2 << endl;
	return 0;
}

4.6 字符串的操作

find 查找字符或字符串
代码语言:javascript
复制
#include<iostream>
using namespace std;

int main()
{
	string s1("hello gugu");
	
	int pos = s1.find('g');

	cout << s1[pos];
	return 0;
}
rfind 从后往前查找字符或字符串
代码语言:javascript
复制
#include<iostream>
using namespace std;

int main()
{
	string s1("hello gugu");
	
	int pos = s1.rfind('g');
	cout << pos << endl;
	cout << s1[pos];
	return 0;
}
c_str 返回C形式的字符串指针
代码语言:javascript
复制
#include<iostream>
using namespace std;

int main()
{
	string s1("hello gugu");
	
	cout << s1.c_str() << endl;
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-05-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、STL是什么?
  • 二、STL的六大组件
  • 三、我们为什么要学string?
    • 3.1 string 的定义
    • 四、string的常用接口使用
      • 4.1 成员函数
        • 构造函数
        • 拷贝构造
        • operator=
      • 4.2 迭代器的使用
        • string 的三种遍历方式
        • rbegin && rend
      • 4.3 容量部分
        • capacity 获取当前容量
        • size 获取当前存储了多少字符
        • resize 减少字符存储,或填充字符
        • reserve 为string扩容
        • clear 清空所有字符
        • empty 判断当前字符串是否为空
        • shrink_to_fit 为当前字符串请求缩容
      • 4.4 元素访问
        • operator[]
      • 4.5 修改
        • += 操作
        • append 追加字符或字符串
        • push_back 尾插
        • assign 替换字符串
        • insert 插入
        • erase 删除字符串的一部分,减少其长度
        • replace 替换
        • swap交换
      • 4.6 字符串的操作
        • find 查找字符或字符串
        • rfind 从后往前查找字符或字符串
        • c_str 返回C形式的字符串指针
    相关产品与服务
    容器服务
    腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档