C++的STL简介(一)

目录

1.什么是STL

2.STL的版本

3.STL的六大组件

 4.string类

4.1为什么学习string类?

4.2string常见接口

4.2.1默认构造

​编辑

4.2.2析构函数

 Element access:

4.2.3 []

4.2.4迭代器

​编辑

 auto

 4.2.4.1 begin和end

 4.2.4.2.regin和rend

Capacity:

 4.2.5.3 size

4.2.6 lenth 

4.2.7 cleart

Modifiers:

4.2.7 apend

4.2.8  +=

4.2.9 erase

4.2.10 replace

 String operations:

 4.2.11 find

4.2.12 substr

4.2.13  find_first_of

4.2.14  find_ last_of

4.2.15 find_first_not_of

 4.2.16 find_last_not_of


1.什么是STL

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

2.STL的版本

  • 原始版本

Alexander Stepanov MengLee 在惠普实验室完成的原始版本,本着开源精神,他们声明允许任何人任意运用,拷贝,修改,传播,商业使用这些代码,无需付费。唯一的条件就是也需要向原始版本一样做开源使用。HP版本--所有STL实现版本的始祖。

  • P.J.版本

由P.J.Plauger开发,继承自HP版本,被Windows Visual C++采用,不能公开或修改,缺陷:可读性比较低,符号命名比较怪异。

  • RW版本

由Rouge Wage 公司开发,继承自HP版本,被C++ Builder 采用 ,不能公开或修改,缺陷:可读性一般

  • SGL版本

由Silicon Graphics Computer Systems ,Inc公司开发,继承自HP版本。被GCC(Linux)采用,可移植性较好,可公开,修改甚至贩卖,从命名风格和编程风格上看,阅读性非常高。

3.STL的六大组件

 4.string类

4.1为什么学习string类?

C语言中,字符串是以"\0"结尾的一些字符的集合,为了方便操作,C标准库中提供了一些str系列的库函数,但是这些库函数与字符串是分开的,不太符合OOP的思想,而且底层空间需要用户自己管理,稍不留神可能还会越界访问。而且在常规工作中,为了简单,方便,快捷,基本都使用string类,很少有人使用C库中的字符串操作函数

4.2string常见接口


4.2.1默认构造

实例

	//无参构造//string();string s1;//带参构造string s2("111");//string(const char* s);	//拷贝构造string s3(s2);//string(const string & str);string s4("123", 2, 1);//string(const string & str, size_t pos, size_t len = npos);//复制str中从字符下标位置 pos 开始的len个 字符进行拷贝构造(如果任一 str 太短或 len 为 string::npos,则复制到str 的末尾)。string s5("123",0 ,string:: npos);//	string(const char* s, size_t n);string s6("123", 2);//从 s 指向的字符数组中复制前 n 个字符。//string(size_t n, char c);//用连续的n个c字符去初始化string s7(3, 'c');//template <class InputIterator>//string(InputIterator first, InputIterator last);
4.2.2析构函数

 Element access:

4.2.3 []

获取字符串的字符

 利用[]来进行读写,下标+[]遍历

int main()
{   string s1("abcd");//写s1[0] ='*';//将下标为0的元素修改为1cout << s1 << endl;//读for (int i = 0; i < s1.size(); i++){cout << s1[i] ;}return 0;
}

 

[]原型

class string
{public:
char&	operator[](size_t i){return _str[i];}
private:char* _str;size_t _size;size_t _capacity;
};
4.2.4迭代器

在 STL 中,迭代器(Iterator)用来访问和检查 STL 容器中元素的对象,它的行为模式和指针类似,但是它封装了一些有效性检查,并且提供了统一的访问格式。他的底层是指针

迭代器遍历

int main()
{   string s1("abcd");string::iterator it = s1.begin();while (it != s1.end()){cout << *it <<" " ;++it;}return 0;
}
 auto

补充一个C++小语法

auto可自动推导类型,极大程度简化代码

const string s3("hello ward!");//string::const_iterator cit=s3.begin(); 可简写成:
auto cit = s3.begin();

 auto声明方式

auto  变量名

auto  函数名 (形参列表)

{

//函数体

}

 auto的实例

int fun()
{return 10;
}
int main()
{int a=10;auto b = a;auto c = 'a';auto d = fun();auto& e = a;auto* f = &a;cout << typeid(a).name() << endl;cout << typeid(b).name()<< endl;cout << typeid(c).name() << endl;cout << typeid(d).name() << endl;cout << typeid(e).name() << endl;cout << typeid(f).name() << endl;return 0;
}

  • 在早期C/C++中auto的含义是:使用auto修饰的变量,是具有自动存储的局部变量,后来这个不重要了,C++11中,标准委员会被废为宝赋予了auto全新的含义即:auto不再是一个存储类型指示符,而是作为一个新的类型指示符来指示编译器,auto声明的变量必须由编译器在编译时期推导而得
  • 用auto声明指针类型时,用auto和auto*没有任何区别,但用auto声明引用类型时必须加&
  • 当在同一行声明多个变量时,这些变量必须是相同的类型,否则编译器会报错,因为编译器实际只对第一个类型进行推导,然后用推导出来的类型定义其他变量
  • auto不能作为函数的参数,可以做返回值,但是谨慎使用
  • aoto不能直接用来声明数组

范围for遍历

 aoto自动推导,字符赋值,自动迭代,自动判断结束,底层上也是迭代器,所有的容器都支持范围for,因为所有的容器都支持迭代器

 4.2.4.1 begin和end

 1.begin

 返回第一个字符的正向迭代器

int main()
{   string s1("abcd");cout<<* s1.begin();return 0;
}

 

2. end 返回最后一个字符的正向迭代器

 可配合起来正向遍历

int main()
{string s1("abcdef");string::const_iterator it = s1.begin();while (it != s1.end()){cout << *it << " ";it++;}return 0;
}

 

 4.2.4.2.regin和rend

regin 返回最后一个的反向迭代器

 rend 返回第一个字符的反向迭代器

配合起来可支持反向遍历

int main()
{string s1("abcdef");string::const_reverse_iterator it = s1.rbegin();while (it != s1.rend()){cout << *it << " ";it++;}return 0;
}

 

Capacity:

 4.2.5.3 size

 返回字符串的长度,不包括'\0'

int main()
{   string s1("abcd");cout << s1.size();return 0;
}

4.2.6 lenth 

  返回以字节为单位的长度,不包括"\0"

int main()
{string s1("abcdef");cout << s1.length()<<endl;return 0;
}

 

4.2.7capacity

l返回容量大小

 

 4.2.7reserve

保留预留,提前开空间,避免扩容,提高效率

int main()
{string s1("abcdef");cout << s1.capacity() << endl;s1.reserve(100);cout << s1.capacity()<<endl;//可以扩容,>=100s1.reserve(50);cout << s1.capacity() << endl;//一般不会缩容return 0;
}

4.2.7 cleart

清除数据,一般不清除容量

int main()
{string s1("abcdef");cout << s1.capacity() << endl;cout << s1.size() << endl;s1.clear();cout << s1.capacity() << endl;cout << s1.size() << endl;//一般不会缩容return 0;
}

 

Modifiers:

4.2.7 apend

字符串追加


int main()
{string s1("abcdef");//string& append(const string & str);s1.append("yyy");cout << s1 << endl;//	string& append(const string & str, size_t subpos, size_t sublen);
//追加 str 子字符串的副本。子字符串是 str 中从字符位置 subpos 开始并跨越 sublen 字符的部分(或者直到 str 的末尾,如果任一 str 太短或 sublen 是 string::npos)。s1.append("aaaa", 2, 1);cout << s1 << endl;return 0;
}

 

4.2.8  +=

字符串拼接,尾插

int main()
{string s1("abcdef");string s2("123");//string& operator+= (const string & str);s1 += s2;s1 += 'a';
//string& operator+= (char c);s1 += "aaa";
//string & operator+= (const char* s);return 0;
}
int main()
{string s1("abcdef");string s2("123");//string& insert(size_t pos, const string & str);// 在pos之前插入strs1.insert(0, "abc");//string& insert(size_t pos, const string & str, size_t subpos, size_t sublen);//在下标pos位置之前插入str下表中subpos到下标sublen位置的元素s2.insert(0, "abcdd", 0, 4);cout << s2 << endl;//string & insert(size_t pos, const char* s);//在pos位置之前插入s//string& insert(size_t pos, const char* s, size_t n);//从在下标为pos的位置插入s的n个字符//string& insert(size_t pos, size_t n, char c);//在pos位置之前插入n个c字符//void insert(iterator p, size_t n, char c);//在迭代器的位置之前插入n个字符c//iterator insert(iterator p, char c);//在迭代器的位置之前插入字符cs1.insert(s1.begin(), '*');cout << s1 << endl;return 0;
}
4.2.9 erase

头删

int main()
{string s1("abcdef");string s2("123");//string& erase(size_t pos = 0, size_t len = npos);//擦除字符串值中从字符位置 pos 开始并到 len 字符的部分不包括len(如果内容太短或 len 为 string::npos,则擦除字符串值的末尾。s1.erase(0, 2);//iterator erase(iterator p);//擦除 p 指向的字符。s1.erase(s1.begin());//iterator erase(iterator first, iterator last);
//擦除[first,last] 范围内的字符序列s1.erase(s1.begin(), s2.end());cout << s1 << endl;return 0;
}
4.2.10 replace

替换

int main()
{string s1("abcdef");string s2("123");//string & replace(size_t pos, size_t len, const char* s);//string& replace(size_t pos, size_t len, const string & str);//把pos位置到len位置替换成str//string& replace(iterator i1, iterator i2, const char* s);//string& replace(iterator i1, iterator i2, const string & str);//把i1到i2之间的迭代器换成str//string& replace(size_t pos, size_t len, size_t n, char c);//string& replace(size_t pos, size_t len, const char* s, size_t n);//把pos位置到len位置替换成str中的前n个//string& replace(iterator i1, iterator i2, const char* s, size_t n);//把i1到i2之间的迭代器换成str中的前n个//string& replace(iterator i1, iterator i2, size_t n, char c);//把i1到i2之间的迭代器换成n个字符c//string& replace(iterator i1, iterator i2,//InputIterator first, InputIterator last);//将迭代器输入到范围内的初始位置和最终位置。使用的范围是 [first,last),它包括 first 和 last 之间的所有字符,包括 first 指向的字符,但不包括 last 指向的字符return 0;
}

 String operations:

 4.2.11 find

查找

返回第一个匹配的第一个字符的位置。
如果未找到匹配项,该函数将返回 string::npos。(整型最大值)

int main()
{string s1("abcdef");string s2("123");//size_t find(const string & str, size_t pos = 0) const;s1.find("bce");//	size_t find(const char* s, size_t pos = 0) const;//在pos位置找ss1.find('a');//	size_t find(const char* s, size_t pos, size_t n) const;//从pos位置找s的前n个cout<< s1.find("aaa", 1, 2);//	size_t find(char c, size_t pos = 0) const;//从pos位置开始搜索字符creturn 0;
}
4.2.12 substr

获得对于位置以后的子串然后重新构成string类返回

int main()
{string s1("abcdef");//tring substr(size_t pos = 0, size_t len = npos) const;//从pos位置开始的len个字符重新构建成string再返回s1.substr(3, 4);return 0;
}

实例

int main()
{string s("text.cpp");size_t pos = s.rfind('.');string suffix = s.substr(pos);cout << suffix << endl;;return 0;
}

 

4.2.13  find_first_of

顺着找字符串中的字符,找到返回第一个出现的下标


int main()
{string s1("abcdef");//ze_t find_first_of(const string & str, size_t pos = 0) const;//ize_t find_first_of(const char* s, size_t pos = 0) const;//ize_t find_first_of(char c, size_t pos = 0) const;//在pos位置开始找str中的字符s1.find_first_of("abc");//ize_t find_first_of(const char* s, size_t pos, size_t n) const;//在pos位置找s的前n个return 0;
}
4.2.14  find_ last_of

倒着找字符串中的字符,找到返回第一个出现的下标

int main()
{string s1("abcdef");//size_t find_last_of(const string & str, size_t pos = npos) const;// //size_t find_last_of(char c, size_t pos = npos) const;// size_t find_last_of(const char* s, size_t pos = npos) const;//从最后一个位置向前找str中的字符s1.find_last_of("Abc",2,4);//size_t find_last_of(const char* s, size_t pos, size_t n) const;//从最后一个位置向前找str中的n个字符return 0;
}

 分割文件

void SplitFilename(const std::string & str){std::cout << "Splitting:" << str << endl;std::size_t found = str.find_last_of(" / \\");std::cout << "path:" << str.substr(0, found) << endl;std::cout << "file:" << str.substr(found + 1) << endl;}
int main()
{string str1("windows\\winhelp.exe");string str2("/url/bin/man");SplitFilename(str1);cout << endl;SplitFilename(str2);return 0;
}

4.2.15 find_first_not_of

没找到就返回,顺着找返回第一个不匹配的对应下标

int main()
{string s1("abcdef");//size_t find_first_not_of(const string & str, size_t pos = 0) const;//size_t find_first_not_of(const char* s, size_t pos = 0) const;	// //size_t find_first_not_of(char c, size_t pos = 0) const;//从第一个位置向前找str中的n个字符找到第一个不匹配的元素下标,找不到就返回// //size_t find_first_not_of(const char* s, size_t pos, size_t n) const;//从第一个位置向后找中第一个不匹配的字符str中的前n个的字符return 0;
}

实例

int main()
{//string str("Please, replace the vowels in this sentence by asterisks.");//除了"abcdef"以外全部替换成*std::size_t found = str.find_first_not_of("abcdef");while (found != std::string::npos){str[found] = '*' ;found = str.find_first_not_of("abcdef", found + 1);}std::cout << str;return 0;
}

 

 4.2.16 find_last_not_of

倒着找,找到第一个不匹配返回下标

int main()
{//ize_t find_first_not_of(const string & str, size_t pos = 0) const;//	size_t find_first_not_of(const char* s, size_t pos = 0) const;//size_t find_first_not_of(char c, size_t pos = 0) const;// 从最后一个位置向前找第一个不匹配str中的字符的下标//size_t find_first_not_of(const char* s, size_t pos, size_t n) const;//从最后一个位置向前找第一个不匹配str中的前n个字符的下标,找不到就返回return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/876307.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

repo中的default.xml文件project name为什么一样?

文章目录 default.xml文件介绍为什么 name 是一样的&#xff0c;path 不一样&#xff1f;总结 default.xml文件介绍 在 repo 工具的 default.xml 文件中&#xff0c;定义了多个 project 元素&#xff0c;每个元素都代表一个 Git 仓库。 XML 定义了多个不同的 project 元素&…

树和二叉树(不用看课程)

1. 树 1.1 树的概念与结构 树是⼀种非线性的数据结构&#xff0c;它是由 n&#xff08;n>0&#xff09; 个有限结点组成⼀个具有层次关系的集合。把它叫做树是因为它看起来像⼀棵倒挂的树&#xff0c;也就是说它是根朝上&#xff0c;而叶朝下的。 • 有⼀个特殊的结点&am…

GD32相较于STM32的优劣势-完全总结

优势 1.更高的主频 GD32单片机的主频可以达到108MHz&#xff0c;‌而STM32的最大主频为72MHz&#xff0c;‌这意味着GD32在代码执行速度上具有优势&#xff0c;‌适合需要快速处理数据的场景 2.更低的内核电压 GD32的内核电压为1.2V&#xff0c;‌而STM32的内核电压为1.8V。…

《系统架构设计师教程(第2版)》第12章-信息系统架构设计理论与实践-05-信息系统架构案例分析

文章目录 1. 价值驱动的体系结构——连接产品策略与体系结构1.1 价值模型1&#xff09;概述2&#xff09;价值驱动因素3&#xff09;传统方法识别价值模型的缺陷&#xff08;了解即可&#xff09; 1.2 体系结构策略&#xff08;挑战&#xff09;1&#xff09; 优先级的确定2&am…

【C++】动态内存管理与模版

目录 1、关键字new&#xff1a; 1、用法&#xff1a; 2、理解&#xff1a; 3、与malloc的相同与不同&#xff1a; 1、相同&#xff1a; 2、不同&#xff1a; 2、模版初阶&#xff1a; 1、函数模版&#xff1a; 1、概念&#xff1a; 2、关键字&#xff1a;template&…

微信公众号获取用户openid(PHP版,snsapi_base模式)

微信公众号获取用户openid的接口有2个&#xff1a;snsapi_base、snsapi_userinfo 详情见微信公众号开发文档&#xff1a;https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html 本文介绍用PHP方式调用snsapi_base接口获取微信用户…

苦学Opencv的第十一天:图像的形态学操作

Python OpenCV从入门到精通学习日记&#xff1a;图像的形态学操作 前言 图像形态学是图像处理中的一个重要分支&#xff0c;主要关注图像中物体的形状和结构。通过形态学操作&#xff0c;我们可以对图像进行有效的分析和处理&#xff0c;例如图像的腐蚀与膨胀、开运算与闭运算…

ansible基础讲解和加密文件讲解

ansible最重要的三个文件 /etc/ansible/ansible.cfg #####ansible的配置文件 /etc/ansible/host ##清单文件inventory ansible-navigator.yml ####以yml结尾的文件可以理解为conf结尾的文件&#xff0c;是配置文件&#xff0c;用于设置剧本playbook playbook讲解 以.yml结…

vue3中计算属性

假如需要修改,需要使用get,set let a ref(111) import {computed} from vue let changeimg computed({get(){return a},set(val){a.value val}}) 如果不需要修改 let a ref(111) import {computed} from vue let changeimg computed(() >{return a })

135.分发糖果,遍历方向+candy选取的详解

力扣135分发糖果 题目思路代码 题目 https://leetcode.cn/problems/candy/description/ 老师想给孩子们分发糖果&#xff0c;有 N 个孩子站成了一条直线&#xff0c;老师会根据每个孩子的表现&#xff0c;预先给他们评分。 你需要按照以下要求&#xff0c;帮助老师给这些孩子…

WordPress原创插件:自定义文章标题颜色

插件设置截图 文章编辑时&#xff0c;右边会出现一个标题颜色设置&#xff0c;可以设置为任何颜色 更新记录&#xff1a;从输入颜色css代码&#xff0c;改为颜色选择器&#xff0c;更方便&#xff01; 插件免费下载 https://download.csdn.net/download/huayula/89585192…

【一图流】Git下载与安装教程

下载Git Git官网&#xff1a;https://git-scm.com/?hlzh-cn 安装Git

UE5 C++跑酷练习(Part2)

一.首先GameMode里有Actor数组&#xff0c;组装直线路&#xff0c;和左右路 #include "CoreMinimal.h" #include "GameFramework/GameModeBase.h" #include "RunGANGameMode.generated.h"UCLASS(minimalapi) class ARunGANGameMode : public AG…

揭秘企业为何钟情定制红酒:品牌形象与不同的礼品的双重魅力

在商务世界的广阔天地里&#xff0c;红酒不仅仅是一种饮品&#xff0c;更是一种传递情感、展示品味的不同媒介。近年来&#xff0c;越来越多的企业开始钟情于定制红酒&#xff0c;其中洒派红酒&#xff08;Bold & Generous&#xff09;通过其品质和个性化的定制服务&#x…

网络访问(Socket/WebSocket/HTTP)

概述 HarmonyOS为用户提供了网络连接功能&#xff0c;具体由网络管理模块负责。通过该模块&#xff0c;用户可以进行Socket网络通滚、WebSocket连接、HTTP数据请求等网络通信服务。 Socket网络通信&#xff1a;通过Socket(嵌套字)进行数据通信&#xff0c;支持的协议包括UDP核…

《追问试面试》系列开篇

我们不管做任何事情&#xff0c;都是需要个理由&#xff0c;而不是盲目去做。 为什么写这个专栏&#xff1f; 就像我们被面试八股文时&#xff0c;市面上有很多面试八股文&#xff0c;随便一个八股文都是500&#xff0c;甚至1000面试题。诸多面试题&#xff0c;难道我们需要一…

基于微信小程序+SpringBoot+Vue的资料分享系统(带1w+文档)

基于微信小程序SpringBootVue的资料分享系统(带1w文档) 基于微信小程序SpringBootVue的资料分享系统(带1w文档) 校园资料分享微信小程序可以实现论坛管理&#xff0c;教师管理&#xff0c;公告信息管理&#xff0c;文件信息管理&#xff0c;文件收藏管理等功能。该系统采用了Sp…

vue3中element tabs标签页 tab-click事件无法拿到最新值

element tabs标签页有2个常用的事件方法&#xff0c;tab-click 和 tab-change tab-click事件 tab-click事件&#xff1a;当用户点击Tab标签时触发&#xff0c;有2个返回参数&#xff0c; (pane: TabsPaneContext, ev: Event) pane.props.name 中可以获取到最新的tab页签绑定值 …

jenkins参数化构建在UI中定义脚本中使用

先看配置&#xff1a; 流水线脚本&#xff1a; pipeline {agent {//label "${server}"label "${28}"}stages {stage(Hello) {steps {echo "--------------------------"// 只有这个可以输出变量echo "${character_argument}"echo &q…

网络通信---TCP协议1

今日内容 三次握手: 指建立tcp连接时&#xff0c;需要客户端和服务端总共发送三次报文确认连接。 四次挥手&#xff1a; 断开一个tcp连接&#xff0c;需要客户端和服务端发送四个报文以确认断开。 编程模型 TCP报文 客户端 服务端