c++学习:STL库(框架)+字符串模板类string+vector容器+list链表

目录

stl库

常用组件包括

字符串库  字符串模板类string

头文件

最常用的字符串模板类

 字符串类型

模板原型

模板的成员数据类型

模板成员函数

有些函数会有重载,可以去下面网址查看std::basic_string - cppreference.comhttps://zh.cppreference.com/w/cpp/string/basic_string

定义字符串模板对象

字符串实战  部分成员函数和非成员函数

vector容器   动态数组类模板

头文件

模板原型

模板的成员数据类型和成员函数

定义动态数组类模板对象

容器实战

简单的学生管理系统

list双向链表

头文件

模板原型

模板得成员数据类型和成员函数

实战


stl库

STL是指标准模板库,提供了通用的模板类和函数,可以实现多种流行和常用的算法和数据结构,例如字符串操作,链表,队列等等

常用组件包括

  • 容器(顺序容器,关联容器)
    • 例如数组,链表,栈,二叉树等等
  • 算法 
    • 作用于容器,提供操作方法,初始化,排序,搜索,转换等等
  • 迭代器
    • 遍历对象集合的元素,类似指针

字符串库  字符串模板类string

头文件

#include <string>

最常用的字符串模板类

  • std::basic_string
    • 为操作任何字符串类型的字符串设计的模板类

 字符串类型

  • 类型                                          定义
  • std::string                                  std::basic_string<char>
  • std::wstring                                std::basic_string<wchar_t>
  • std::u8string (C++20 起)            std::basic_string<char8_t>
  • std::u16string (C++11 起)          std::basic_string<char16_t>
  • std::u32string (C++11 起)          std::basic_string<char32_t>

模板原型

template<
    class CharT,//字符串类型
    class Traits = std::char_traits<CharT>,//指定字符串类型上操作的特性类
    class Allocator = std::allocator<CharT>//用于分配内存存储的分配器类型
>class basic_string;

模板的成员数据类型

成员类型定义
traits_typeTraits
value_typeCharT
allocator_typeAllocator
size_type
Allocator::size_type
std::allocator_traits<Allocator>::size_type
difference_type
Allocator::difference_type
std::allocator_traits<Allocator>::difference_type
referencevalue_type&
const_referenceconst value_type&
pointer
Allocator::pointer
std::allocator_traits<Allocator>::pointer
const_pointer
Allocator::const_pointer
std::allocator_traits<Allocator>::const_pointer
iterator

指向 value_type 的老式随机访问迭代器 (LegacyRandomAccessIterator) 及老式连续迭代器 (LegacyContiguousIterator)

指向 value_type 的老式随机访问迭代器 (LegacyRandomAccessIterator) 、contiguous_iterator 及常量表达式迭代器 (ConstexprIterator)

const_iterator

指向 const value_type 的老式随机访问迭代器 (LegacyRandomAccessIterator) 及老式连续迭代器 (LegacyContiguousIterator)

指向 const value_type 的老式随机访问迭代器 (LegacyRandomAccessIterator) 、contiguous_iterator 及常量表达式迭代器 (ConstexprIterator)

reverse_iteratorstd::reverse_iterator<iterator>
const_reverse_iteratorstd::reverse_iterator<const_iterator>

模板成员函数

成员函数
(构造函数)    构造 basic_string(公开成员函数)
(析构函数)    销毁字符串,在使用内部存储时解分配它(公开成员函数)
operator=    为字符串赋值(公开成员函数)
assign    赋值字符给字符串(公开成员函数)
assign_range    赋值范围内的字符到字符串(公开成员函数)
get_allocator    返回关联的分配器(公开成员函数)

元素访问
at    访问指定字符,有边界检查(公开成员函数)
operator[]    访问指定字符(公开成员函数)
front    访问首字符(公开成员函数)
back    访问最后的字符(公开成员函数)
data    返回指向字符串首字符的指针(公开成员函数)
c_str    返回字符串的不可修改的 C 字符数组版本(公开成员函数)
operator basic_string_view    返回到整个字符串的不可修改的 basic_string_view(公开成员函数)

迭代器
begin、cbegin    返回指向起始的迭代器(公开成员函数)
end、cend    返回指向末尾的迭代器(公开成员函数)
rbegin、crbegin    返回指向起始的逆向迭代器(公开成员函数)
rend、crend    返回指向末尾的逆向迭代器(公开成员函数)

容量
empty    检查字符串是否为空(公开成员函数)
size、length    返回字符数(公开成员函数)
max_size    返回字符数的最大值(公开成员函数)
reserve    保留存储(公开成员函数)
capacity    返回当前对象分配的存储空间能保存的字符数量(公开成员函数)
shrink_to_fit    通过释放不使用内存减少内存使用(公开成员函数)

操作
clear    清除内容(公开成员函数)
insert    插入字符(公开成员函数)
insert_range    插入范围内的字符(公开成员函数)
erase    移除字符(公开成员函数)
push_back    后附字符到结尾(公开成员函数)
pop_back    移除末尾字符(公开成员函数)
append    后附字符到结尾(公开成员函数)
append_range    后附范围内的字符到结尾(公开成员函数)
operator+=    后附字符到结尾(公开成员函数)
compare    比较二个字符串(公开成员函数)
starts_with    检查字符串是否始于给定前缀(公开成员函数)
ends_with    检查字符串是否终于给定后缀(公开成员函数)
contains    检查字符串是否含有给定的子串或字符(公开成员函数)
replace    替换字符串的指定部分(公开成员函数)
replace_with_range    以范围中的字符替换字符串的指定部分(公开成员函数)
substr    返回子串(公开成员函数)
copy    复制字符(公开成员函数)
resize    更改存储的字符数(公开成员函数)
resize_and_overwrite    更改存储的字符数并可能经由用户提供的操作重写不确定的内容(公开成员函数)
swap    交换内容(公开成员函数)

查找
find    于字符串中寻找字符(公开成员函数)
rfind    寻找子串的最后一次出现(公开成员函数)
find_first_of    寻找字符的首次出现(公开成员函数)
find_first_not_of    寻找字符的首次缺失(公开成员函数)
find_last_of    寻找字符的最后一次出现(公开成员函数)
find_last_not_of    寻找字符的最后一次缺失(公开成员函数)

有些函数会有重载,可以去下面网址查看std::basic_string - cppreference.comicon-default.png?t=N7T8https://zh.cppreference.com/w/cpp/string/basic_string

定义字符串模板对象

#include <string>

using namespace std;

int main()
{
    //实例化一个STL库中的字符串类模板的对象
    std::basic_string<char>  s1;
    basic_string<char>  s2;
    std::string  s3;
    string s4;
}

上面的s1,s2,s3,s4定义其实都是一样的,c++为了方便,将std::basic_string<char>另起名为std::string

typedef std::basic_string<char> std::string;

字符串实战  部分成员函数和非成员函数

#include <iostream>
#include <string>

using namespace std;

int main()
{
    //实例化一个STL库中的字符串类模板的对象
    std::basic_string<char>  s1;
    basic_string<char>  s2;
    std::string  s3;
    string s4;

    //构造
    string s5("hello world");
    //赋值重载=
    s4 = s5;
    //元素访问,返回引用
    cout<<s4.at(1)<<endl;
    s4.at(0) = 'x';
    //赋值重载[]
    s4[2] = 'p';
    //赋值重载<<
    cout<<s4<<endl;

    //data返回的是  string转化为的const char*指针
    //返回指向字符串首字符的指针
    cout<<"s5:"<<s5.data()<<endl;
    //返回字符串的不可修改的 C 字符数组版本
    cout<<"s5:"<<s5.c_str()<<endl;

    //通过迭代器 遍历 容器中的每个元素
    //begin返回字符串首字符的迭代器 数据类型是iterator 
    //end返回字符串最后字符的迭代器 数据类型是iterator 
    //iterator在类模板里  正常写法是std::basic_string<char>::iterator 
    for(string::iterator it=s5.begin();it!=s5.end() ;it++)
    {
        cout<<*it<<endl;
    }

    //容量
    //判断是否为空,返回blue类型
    string s6;
    if(s6.empty())
    {
        cout<<"s6.empty"<<endl;
    }
    //capacity返回当前这个对象里面的指针成员 指向的堆空间,能够容纳存储多少个字符
    cout<<s6.capacity()<<endl;

    //size、length当前这个对象中有效的字符串的长度
    cout<<"size:"<<s6.size()<<endl;
    cout<<"length:"<<s6.length()<<endl;

    //插入操作
    string s77("nihao");
    s77.insert(2,1,'a');
    cout<<s77<<endl;//niahao
    //尾插法
    //nihao---->nihao,zaime
    //push_back只能插一个字符
    string s7("nihao");
    s7.push_back(',');
    s7.push_back('z');
    s7.push_back('a');
    s7.push_back('i');
    s7.push_back('m');
    s7.push_back('e');
    cout<<s7<<endl;

    //尾部删除删除
    s7.pop_back();
    cout<<s7<<endl;

    //append追加
    string s8("hello");
    s8.append(" world");
    cout<<s8<<endl;
    //赋值重载+=
    s8 += "123456";
    cout<<s8<<endl;

    //返回子串
    //要求 将 s8("hello world123456")-->将world返回来
    cout<<s8.substr(6,5)<<endl;

    //查找
    string s9("abc123456efg");
    //想要在上面的字符串中查找 是否存在123456
    int ret = s9.find("www");
    //如果找到了,则返回 子串的起始位置 int ,没有找到返回 -1
    cout<<ret<<endl;


    //以下是类的  非成员函数 ,不能通过对象进行调用
    string s10("hello");
    if(s10 == "hello") //等同于调用operator==(s10,"hello")
    {

    }

    string s11("123456");
    //将string 转换成 int
    int val = std::stoi(s11);

    //将整型 100转换成 string
    int data = 100;
    string s12 = std::to_string(data);
    cout<<s12<<endl;

    return 0;
}

vector容器   动态数组类模板

std::vector是封装动态数组的顺序容器,简单来说就是动态数组类模板

头文件

#include<vector>

模板原型

template<
    class T,//元素的类型
    class Allocator = std::allocator<T>//用于获取/释放内存及构造/析构内存中元素的分配器
> class vector;

模板的成员数据类型和成员函数

std::vector - cppreference.comicon-default.png?t=N7T8https://zh.cppreference.com/w/cpp/container/vector

定义动态数组类模板对象

    std::vector<int> v1(10);

容器实战

#include <iostream>
#include <vector>

using namespace std;

//ostream& operator<<(ostream&out, std::vector<int> &ra)
//{
//    for(int i=0; i<ra.size(); i++)
//    {
//        out<<ra.at(i)<<"\t";
//    }
//}

int main()
{
    //实例化一个STL库中的动态数组类模板的对象
    //注意:如果实例化vector类对象的时候,没有指定元素的个数
    //那么,容器里面默认的大小为0,也就是没有空间
    std::vector<int> v1(10);

    for(int i=0; i<10; i++)
    {
        //v1.at(i) = i+100;
        v1[i] = i+10;
    }

    //编译器 会转换 成 运算符函数 operator<<(cout,v1)需要自己定义
    //cout<<v1<<endl; 
    
    //返回首元素指针
    int *arr = v1.data();

    //通过迭代器遍历容器
    //正向迭代器  需要将运算符函数 operator<<注释掉,要不然测不出来
    std::vector<int>::iterator  it;
    for(it=v1.begin(); it!=v1.end(); it++){
        cout<<*it<<"\t";
    }
    cout<<endl;

    //反向迭代器遍历容器
    std::vector<int>::reverse_iterator rit;
    for(rit = v1.rbegin(); rit!=v1.rend(); rit++)
    {
        cout<<*rit<<"\t";
    }cout<<endl;

    //尾插 并且 会给你进行扩容
    v1.push_back(1000);
    //遍历查看
    for(it=v1.begin(); it!=v1.end(); it++){
        cout<<*it<<"\t";
    }cout<<endl;

    return 0;
}

简单的学生管理系统

#include <iostream>
#include <vector>

using namespace std;

struct Student{
    string name;
    int age;
    float score;
};

int main()
{
    std::vector<struct Student> v;

    struct Student s1 = {"zhang3",22,100};
    v.push_back(s1);

    struct Student s2 = {"zhang4",22,100};
    v.push_back(s2);

    struct Student s3 = {"zhang5",22,100};
    v.push_back(s3);

    for(std::vector<struct Student>::iterator it=v.begin(); it!=v.end(); it++)
    {
        cout<<"name:"<<it->name<<" age:"<<it->age<<" score:"<<it->score<<endl;
    }

    return 0;
}

list双向链表

头文件

 #include<list>

模板原型

template<
    class T,//元素的类型
    class Allocator = std::allocator<T>//用于获取/释放内存及构造/析构内存中元素的分配器
> class list;

模板得成员数据类型和成员函数

std::list - cppreference.comicon-default.png?t=N7T8https://zh.cppreference.com/w/cpp/container/list

实战

#include <iostream>
#include <list>

using namespace std;

class Student
{ 
public:
    Student(string n,int a,float s):name(n),age(a),score(s){}

    void show()
    {
        cout<<name<<"\t"<<age<<"\t"<<score<<endl;
    }
private:
    string name;
    int age;
    float score;
};



int main()
{

    //实例化一个双向链表容器的对象
    std::list<int> list1;

    //尾插
    list1.push_back(10);
    list1.push_back(20);
    list1.push_back(30);
    list1.push_back(40);
    list1.push_back(50);

    // 使用[]+ 下标的方式 访问 容器,一般来说,这个容器的每个元素的内存空间一定是连续的
    // int arr[3];    arr[2]等同于*(arr+2)
    //list1[0] =  1000;  错误 ,链表的内存空间不是连续的,不能使用 [ ]

    //迭代器遍历
    std::list<int>::iterator it;
    for(it=list1.begin(); it!=list1.end(); it++)
    {
        cout<<*it<<"\t";
    }cout<<endl;

    //学生信息
    std::list<Student> list;
    list.push_back(Student("zhang3",22,100));
    list.push_back(Student("zhang4",25,100));
    list.push_back(Student("zhang5",26,100));
    list.push_back(Student("zhang6",27,100));

    std::list<Student>::iterator it;
    for(it=list.begin(); it!=list.end(); it++)
    {
        it->show();
    }

    return 0;
}

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

探索C语言中的水仙花数及其计算方法

在计算机科学与数学的交叉领域中&#xff0c;有一种特殊的整数被称为“水仙花数”&#xff0c;它是指一个三位数&#xff0c;其各位数字立方和等于该数本身。例如&#xff0c;153是一个典型的水仙花数&#xff0c;因为1 5 3 1 125 27 153。 下面&#xff0c;我们通过一段…

NODE笔记 0

一些简单的node学习笔记记录&#xff0c;是Vue等前端框架的基础 入门学习备忘录 文章目录 前言一、pandas是什么&#xff1f;二、使用步骤 1.引入库2.读入数据总结 前言 node.js 内置网络服务器&#xff0c;是前端框架学习的基础&#xff1a; 概念&#xff1a;…

Pytorch框架学习笔记

官网- PyTorch Tensor 构造随机初始化矩阵 xtorch.rand(5,3) 构造全0矩阵&#xff0c;数据类型为long xtorch.zeros&#xff08;5,3,dtypetorch.long&#xff09; 获取维度信息 x.size(&#xff09; tensor加法 torch.add&#xff08;x&#xff0c;y&#xff09; xy y…

linux常见操作,and一些练习题加线上练习网站,无须配置linux.持续更新中。。。。

文章目录 cd命令相对路径特殊的路径表达符和cd一起使用pwdmore 查看文件内容支持翻页小技巧clear用户&#xff0c;用户权限 and 用户的切换如何创建用户 ls和通配符的使用利用通配符 *grep 过滤管道符 |如何学习Linux在线练习网站 https://www.lanqiao.cn/courses/1 cd命令 cd…

【QML COOK】- 004-添加动画

1. 编辑main.qml import QtQuickWindow {width: 800height: 800visible: truetitle: qsTr("Hello World")Image {id: backgroudanchors.fill: parentsource: "qrc:/Resources/Images/arrow.png"Behavior on rotation {NumberAnimation {duration: 1000}}}…

IntelliJ IDEA远程查看修改Ubuntu上AOSP源码

IntelliJ IDEA远程查看修改Ubuntu上的源码 本人操作环境windows10,软件版本IntelliJ IDEA 2023.2.3&#xff0c;虚拟机Ubuntu 22.04.3 LTS 1、Ubuntu系统安装openssh 查看是否安装&#xff1a; ssh -V 如果未安装&#xff1a; sudo apt install openssh-server # 开机自启…

建模软件Rhinoceros mac介绍说明

Rhinoceros mac是一款3D设计软件“犀牛”&#xff0c;在当今众多三维建模软件中&#xff0c;Rhinoceros 版因为其体积小、功能强大、对硬件要求低而广受欢迎&#xff0c;对于专业的3D设计人员来说它是一款不错的3D建模软件&#xff0c;Rhinoceros Mac中文版能轻易整合3DS MAX与…

环信IM Demo登录方式如何修改为自己项目的?

在环信即时通讯云IM 官网下载Demo&#xff0c;本地运行只有手机验证码的方式登录&#xff1f;怎么更改为自己项目的Appkey和用户去进行登录呢&#xff1f; &#x1f447;&#x1f447;&#x1f447;本文以Web端为例&#xff0c;教大家如何更改代码来实现 1、 VUE2 Demo vue2…

【小白专用】C#关于角色权限系统

&#xff08;C#&#xff09;用户、角色、权限 https://www.cnblogs.com/huangwen/articles/638050.html 权限管理系统——数据库的设计&#xff08;一&#xff09; https://www.cnblogs.com/cmsdn/p/3371576.html 权限管理系统——菜单模块的实现&#xff08;二&#xff09; …

Flutter 监听前台和后台切换的状态

一 前后台的切换状态监听 混入 WidgetsBindingObserver 这个类&#xff0c;这里提供提供了程序状态的一些监听 二 添加监听和销毁监听 overridevoid initState() {super.initState();//2.页面初始化的时候&#xff0c;添加一个状态的监听者WidgetsBinding.instance.addObserver…

Selenium 学习(0.17)——软件测试之流程图绘制方法

病假5天&#xff0c;出去野20天&#xff0c;成功错过了慕课网上的期末考试。 害&#xff0c;都怪玩乐太开心了…… 反正咱又不指着全靠这个行当来吃饭&#xff0c;错过也就错过了&#xff0c;立的Flag能抢救一下还是要抢救一下吧。【这个其实早都会画了&#xff0c;而且基本也正…

Python实现PowerPoint(PPT/PPTX)到PDF的批量转换

演示文稿是一种常见传达信息、展示观点和分享内容的形式&#xff0c;特别是PowerPoint演示文稿&#xff0c;广泛应用于各行各业&#xff0c;几乎是演讲等场合的必备工具。然而&#xff0c;演示文稿也有其限制&#xff0c;对设备的要求较高&#xff0c;且使用不同的软件或设备演…

分布式之任务调度Elastic-Job学习二

4 Spring 集成与分片详解 ejob-springboot 工程 4.1 pom 依赖 <properties><elastic-job.version>2.1.5</elastic-job.version> </properties> <dependency><groupId>com.dangdang</groupId><artifactId>elastic-job-lite-…

每日一练:LeeCode-101. 对称二叉树【二叉树】

本文是力扣LeeCode-101. 对称二叉树 学习与理解过程&#xff0c;本文仅做学习之用&#xff0c;对本题感兴趣的小伙伴可以出门左拐LeeCode。 给你一个二叉树的根节点 root &#xff0c; 检查它是否轴对称。 提示&#xff1a; 树中节点数目在范围 [1, 1000] 内 -100 < Node.v…

通过盲对抗性扰动实时击败基于DNN的流量分析系统

文章信息 论文题目&#xff1a;Defeating DNN-Based Traffic Analysis Systems in Real-Time With Blind Adversarial Perturbations 期刊&#xff08;会议&#xff09;&#xff1a;30th USENIX Security Symposium 时间&#xff1a;2021 级别&#xff1a;CCF A 文章链接&…

理论U3 决策树

文章目录 一、决策树算法1、基本思想2、构成1&#xff09;节点3&#xff09;有向边/分支 3、分类步骤1&#xff09;第1步-决策树生成/学习、训练2&#xff09;第2步-分类/测试 4、算法关键 二、信息论基础1、概念2、信息量3、信息熵&#xff1a; 二、ID3 (Iterative Dichotomis…

Python+requests搭建接口自动化测试框架

一、接口自动化的意义&#xff08;为什么做这个框架&#xff09; 新版本上线时之前版本的功能需要进行回归测试&#xff0c;导致大量的重复性手工测试。引入自动化测试可以使用自动化技术代替部分手工的回归性测试&#xff0c;解放更多人力做其它更有必要的事情。但目前项目UI变…

抖去推账号矩阵+无人直播+文案引流系统开发搭建--开源

核心技术 1. AI自动直播&#xff1a; 智能系统通过丰富可定制的文案库&#xff0c; 拥有有料有趣的灵魂。不仅能自动语音讲解内容&#xff0c;还可以在直播中和用户灵活互动。直播中可将团购商品同话术自动上下架。 2. AI剪辑 可一键智能批量成片&#xff0c;也可跟着模板剪…

书生·浦语大模型全链路开源体系 学习笔记 第二课

基础作业&#xff1a; 使用 InternLM-Chat-7B 模型生成 300 字的小故事&#xff08;需截图&#xff09;。熟悉 hugging face 下载功能&#xff0c;使用 huggingface_hub python 包&#xff0c;下载 InternLM-20B 的 config.json 文件到本地&#xff08;需截图下载过程&#xf…

基于STM32和MPU6050的自平衡小车设计与实现

基于STM32和MPU6050的自平衡小车设计和实现是一个有趣而具有挑战性的项目。在本文中&#xff0c;我们将介绍如何利用STM32微控制器和MPU6050传感器实现自平衡小车&#xff0c;并提供相应的代码示例。 1. 硬件设计 自平衡小车的核心硬件包括STM32微控制器、MPU6050传感器以及电…
最新文章