重学C++系列之STL库

news2024/9/20 18:00:22

一、什么是STL库

        STL是“Standard Template Library”的缩写,中文翻译为“标准模板库”。C++STL是一套功能强大的C++模板类,提供了通用的模板类和函数,这些模板类和函数可以实现多种流行和常用的算法和数据结构,如字符串操作、链表、队列、栈。

        C++标准模板库的核心包括以下三个组件:

C++标准模板库组件
组件描述
容器(顺序容器、关联容器)容器是用来管理某一类对象的集合。C++提供了各种不同类型的容器,比如list, vector, map
算法算法作用于容器,提供了执行各种操作的方式,包括对容器内容执行初始化、排序、搜索和转换等操作
迭代器迭代器用于遍历对象集合的元素。类似于指针

二、STL的使用案例

        1、可以去看官网的操作文档,C++中文参考手册

        2、字符串模板类string

                加上头文件#include <string>

#include <iostream>
#include <string>
using namespace std;

int main()
{
    // 1、定义
    string s1("hello");
    string s2 = "world";
    cout << "s1 = " << s1 << endl;
    cout << "s2 = " << s2 << endl;

    // 2、赋值
    string s3 = s1;
    string s4 = s1 + s2;
    string s5 = s1;
    s5 += s2;
    cout << "s3 = " << s3 << endl;
    cout << "s4 = " << s4 << endl;
    cout << "s5 = " << s5 << endl;

    // 3、at, 返回某个位置的引用
    string s6 = s1;
    s6.at(0) = 'p';
    cout << "s6 = " << s6 << endl;

    // 4、容量
    string s7 = s1;
    cout << "s7.size() = " << s7.size() << endl;
    cout << "s7.capacity() = " << s7.capacity() << endl;
    cout << "s7.length() = " << s7.length() << endl;
    string s8 = s1+s1+s1+s1+s1+s1;
    cout << "s8.capacity1() = " << s8.capacity() << endl;


    // 5、第一个字符和最后一个字符
    cout << "s1第一个字符" << s1.front() << endl;
    cout << "s1最后一个字符" << s1.back() << endl;

    // 6、返回字符串类中,字符串的指针地址
    const char* s_data = s1.data();
    const char* s_str = s1.c_str();

    cout << "s_data = " << s_data << endl;
    cout << "s_str = " << s_str << endl;

    // 7、插入
    string s9 = s1;
    s9.insert(0, "abcd");
    s9.insert(2, "xyz");
    cout << "s9 = " << s9 << endl;

    // 8、[]
    s1[0] = 'x';
    cout << "s1 = " << s1 << endl;

    // 9、追加
    string s10 = s1;
    s10.push_back('+');
    s10.append("xyz");
    cout << "s10 = " << s10 << endl;

    // 10、查找
    int index = s1.find("o");
    cout << "index = " << index << endl;

    // 11、比较
    string s12 = "hello";
    if(s12 == "hello")
    {
        cout << "s12 == hello" << endl;
    }

    // 12、迭代器
    string s13 = "hello world";
    cout << "s13 = ";
    for(string::iterator it = s13.begin(); it != s13.end(); it++)
    {
        cout << *it;
    }
    cout << endl;

    // 13、数值转换
    string s14 = "123456";
    int value = stoi(s14);
    cout << "value = " << value << endl;
    string s15 = to_string(value);
    cout << "s15 = " << s15 << endl;

    // 14、 获取子串
    string s16 = s13.substr(0, 5);
    cout << "s16 = " << s16 << endl;


    return 0;
}

        3、顺序容器vector

                  加上头文件#include <vector>

#include <iostream>
#include <vector>
using namespace std;

struct node 
{
    char *name;
    void func()
    {

    }
};




int main()
{
    vector<int> myvector;

    // 1、插入
    myvector.push_back(10);
    myvector.push_back(20);
    myvector.push_back(30);
    myvector.push_back(40);
    myvector.push_back(50);

    // 2、迭代器遍历
    vector<int>::iterator it;
    for(it = myvector.begin(); it != myvector.end(); it++)
    {
        cout << *it << '\t';
    }
    cout << endl;

    // 3、删除
    cout << "after del:" << endl;
    myvector.pop_back();
    myvector.pop_back();
   

    // 4、容量
    cout << "size = " << myvector.size() << endl;

    // 5、使用数组形式遍历
    for(int i = 0; i < myvector.size(); i++)
    {
        cout << myvector.at(i) << '\t';
    }
    cout << endl;


    return 0;
}

        4、双向链表容器list

                 加上头文件#include <list>

#include <iostream>
#include <list>


using namespace std;


class Student
{
private:
    string name;
    int age;
    int score;
public:
    Student(string name = string(), int age = 18, int score = 30)
    {
        this->age = age;
        this->name = name;
        this->score = score;
    }
    void show()
    {
        cout << name << '\t' << age << '\t' << score << endl;
    }
    friend bool cmp(const Student &s1, const Student &s2);
    string getName()const
    {
        return name;
    }
    void setAge(int Age)
    {
        this->age = Age;
    }
};

bool cmp(const Student &s1, const Student &s2)
{
    return s1.score > s2.score;
}



int main()
{
    list<Student> mylist;
    // 插到后面,尾插
    mylist.push_back(*(new Student("zhang3", 20, 90)));
    mylist.push_back(*(new Student("zhang4", 21, 79)));

    // 插到前面,头插
    mylist.push_front(*(new Student("zhang7", 19, 89)));
    mylist.push_front(*(new Student("zhang8", 25, 80)));

    // 遍历,用迭代器
    cout << "before sort" << endl;
    list<Student>::iterator it;
    for(it = mylist.begin(); it != mylist.end(); it++)
    {
        it->show();
        
    }

    // 排序
    mylist.sort(cmp);   // cmp函数是自定义的排序内容

    cout << "after sort" << endl;
    for(it = mylist.begin(); it != mylist.end(); it++)
    {
        it->show();
    }

    // 删除
    it = mylist.begin();
    it = mylist.erase(it);
    cout << "after del first" << endl;
    for(it = mylist.begin(); it != mylist.end(); it++)
    {
        it->show();
    }

    // 查找和修改
    for(it = mylist.begin(); it != mylist.end(); it++)
    {
        if(it->getName() == "zhang7")
        {
            cout << "find zhang7" << endl;
            it->setAge(50);
        }
    }

    for(it = mylist.begin(); it != mylist.end(); it++)
    {
        it->show();
        
    }


    return 0;
}

5、栈容器stack

#include <iostream>
#include <stack>

using namespace std;



int main()
{
    stack<int> mystack;
    // 入栈
    mystack.push(10);
    mystack.push(20);
    mystack.push(30);
    mystack.push(40);
    mystack.push(50);

    // 栈的元素个数
    cout << "size:" << mystack.size() << endl;

    // 栈没有空,就一直出栈
    int data;
    while(!mystack.empty())
    {
        data = mystack.top();   // 出栈前需要先获取栈顶元素
        mystack.pop();
        cout << data << "\t";
    }
    cout << endl;

    return 0;
}

6、队列容器queue

#include <iostream>
#include <queue>

using namespace std;



int main()
{
    queue<int> myqueue;

    // 入队
    myqueue.push(10);
    myqueue.push(20);
    myqueue.push(30);
    myqueue.push(40);
    myqueue.push(50);

    // 获取队列的元素个数 
    cout << "size: " << myqueue.size() << endl;
    // 队头元素
    cout << "front: " << myqueue.front() << endl;
    // 队尾元素
    cout << "back: " << myqueue.back() << endl;

    // 遍历
    int data;
    while(!myqueue.empty())
    {
        data = myqueue.front();
        myqueue.pop();  // 从队头出队的,先要保留队头元素
        cout << data << "\t";
    }
    cout << endl;

    return 0;
}

7、关联容器map 

#include <iostream>
#include <map>

using namespace std;



int main()
{
    // map是以键值对的方式存放数据
    // 第一个类型是键,第二类型是值,其中键不一定是整形可以是字符串
    map<int, string> mymap1;

    // 插入或者访问,键不一定要连续
    mymap1[1] = "a";
    mymap1[2] = "b";
    mymap1[10] = "c";
    mymap1[100] = "d";

    for(map<int, string>::iterator it = mymap1.begin(); it != mymap1.end(); it++)
    {
        // 键用first来访问,值用second来访问
        // 不允许使用cout << it << endl; 来访问
        cout << it->first << " = " << it->second << endl;
    }

    // 不一定要连续
    map<string, string> mymap2;
    mymap2["zhang3"] = "123";
    mymap2["li4"] = "124";
    mymap2["wang5"] = "45";
    mymap2["hong6"] = "4543";

    for(map<string, string>::iterator it = mymap2.begin(); it != mymap2.end(); it++)
    {
        // 键用first来访问,值用second来访问
        cout << it->first << " = " << it->second << endl;
    }

    return 0;
}

三、总结

        以上就是STL库中常用的容器以及对应的操作,使用时需要添加对应的头文件名,同时不同容器之间的有些相同操作是同名的,具体更多细节可以去看看官网的中文参考手册。

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

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

相关文章

mybatisJava对象、list和json转换

1. 参考mybatis-plus mybatis Java对象、list和json转换 网上好多不靠谱&#xff0c;参考mybatis-plus中TableField&#xff0c;mybatis中自定义实现 这样不需要对象中属性字符串接收&#xff0c;保存到表中&#xff0c;都是转义字符&#xff0c;使用时还要手动转换为对象或者…

【UI自动化测试】Jenkins配置

前一段时间帮助团队搭建了UI自动化环境&#xff0c;这里将Jenkins环境的一些配置分享给大家。 背景&#xff1a; 团队下半年的目标之一是实现自动化测试&#xff0c;这里要吐槽一下&#xff0c;之前开发的测试平台了&#xff0c;最初的目的是用来做接口自动化测试和性能测试&…

棱镜七彩成为“软件供应链安全推进工作组”首批成员单位

2023年7月29日&#xff0c;由基础软件质量控制与技术评价工业和信息化部重点实验室&#xff08;以下简称“实验室”&#xff09;举办的软件供应链安全研讨会暨软件供应链安全推进工作组成立会议在京成功召开。 会上&#xff0c;实验室委托中国软件评测中心&#xff08;工业和信…

助力质检维护,基于超轻量级分割模型ege-unet开发构建水泥基建裂缝分割识别系统

在前面的博文&#xff1a; 《参数量仅有50KB的超轻量级unet变种网络egeunet【参数和计算量降低494和160倍】医疗图像分割实践》 初步学习和实践了最新的超轻量级的unet变种网络在医疗图像领域内的表现&#xff0c;在上文中我们就说过会后续考虑将该网络模型应用于实际的生产业…

GNN+RA 文献阅读-- GNN对RA的建模

简述&#xff1a;主要是几篇 如何利用GNN 对 资源分配进行建模的paper&#xff0c;【1】【2】都是对无线链路建模&#xff0c;【3】比较有参考性&#xff0c;【4】偏于RL&#xff0c;对GNN表述模糊。 用GNN建模网络的思路&#xff1a; 1.Graph 是有向图还是无向图&#xff1f…

HarmonyOS 开发基础(二)组件拼凑简单登录页面

一、简单登录页面 Entry Component /* 组件可以基于struct实现&#xff0c;组件不能有继承关系&#xff0c;struct可以比class更加快速的创建和销毁。*/ struct Index {State message: string Hello Worldbuild() {// https://developer.harmonyos.com/cn/docs/documentation/…

万应低代码 7 月重点更新内容速递

速览版 详情版 低代码开发能力提升 业务逻辑 业务逻辑是什么&#xff1f; 在万应低代码中&#xff0c;「业务逻辑」指的是应用程序中的核心规则和功能&#xff0c;它决定了数据如何被处理和操作。就像搭积木一样&#xff0c;业务逻辑告诉计算机在特定情况下如何运行和响应。比…

【C语言】操作符----详解

&#x1f341; 博客主页:江池俊的博客 &#x1f4ab;收录专栏&#xff1a;C语言——探索高效编程的基石 &#x1f4bb; 其他专栏&#xff1a;数据结构探索 &#x1f4a1;代码仓库&#xff1a;江池俊的代码仓库 &#x1f3aa; 社区&#xff1a;C/C之家社区 &#x1f341; 如果觉…

【投资笔记】美股要变天了?

美股上涨&#xff0c;但是风险溢价创新低 7月&#xff0c;标普和纳指创两年依赖首次5个月连涨&#xff0c;但是风险溢价创二十年新低&#xff1b;https://asset.wsj.net/dynamic-insets/charts/cdc_5387e4742a1ca607d6defc38_embed.html 如果这个指标为正&#xff0c;说明投资…

视频媒体有哪些?视频媒体采访服务怎么做?

传媒如春雨&#xff0c;润物细无声&#xff0c;大家好&#xff0c;我是51媒体网胡老师。 一&#xff0c;在国内&#xff0c;主流的视频媒体包括&#xff1a; 1. 电视台&#xff1a;包括国家级、地方性和专业性电视频道&#xff0c;涵盖各类新闻、综艺、娱乐、体育等节目。 2…

链表OJ:环形链表

Lei宝啊&#xff1a;个人主页 愿所有美好与我们不期而遇 题目描述 &#xff1a; 给你一个链表的头节点 head &#xff0c;判断链表中是否有环。 接口&#xff1a; bool hasCycle(struct ListNode *head) 示例1&#xff1a; 示例2&#xff1a; 返回值&#xff1a; true或…

电力系统基础知识(东方电子)持续更新

文章目录 三相电、相电压、线电压断路器和继电器电压互感器、电流互感器GOOSE、SV 三相电、相电压、线电压 因为交流电可以通过变压器升降&#xff0c;很容易实现远距离输电&#xff0c;而直流电无法升降&#xff0c;远距离输电会造成巨大浪费。 三相电&#xff1a;三相交流电…

go程序使用tcp短连接报:only one usage of each socket address

环境及现象 Win10上位机&#xff08;C#,WPF&#xff09;后台使用go作为服务。 连接情况 C#连接大概60个TCP长连接&#xff08;设备&#xff09;。 后台go服务连接60个UDP短连接&#xff08;设备附属硬件&#xff09;&#xff0c; 10个TCP短连接&#xff08;PLC,modbus通讯&a…

【python】使用Selenium和Chrome WebDriver来获取 【腾讯云 Cloud Studio 实战训练营】中的文章信息

文章目录 前言导入依赖库设置ChromeDriver的路径创建Chrome WebDriver对象打开网页找到结果元素创建一个空列表用于存储数据遍历结果元素并提取数据提取标题、作者、发布时间等信息判断是否为目标文章提取目标文章的描述、阅读数量、点赞数量、评论数量等信息将提取的数据存储为…

坚鹏:中国邮储银行金融科技前沿技术发展与应用场景第2期培训

中国邮政储蓄银行金融科技前沿技术发展与应用场景第2期培训圆满结束 中国邮政储蓄银行拥有优良的资产质量和显著的成长潜力&#xff0c;是中国领先的大型零售银行。2016年9月在香港联交所挂牌上市&#xff0c;2019年12月在上交所挂牌上市。中国邮政储蓄银行拥有近4万个营业网点…

ByteBuffer

ByteBuffer 1.创建方式创建方式1:ByteBuffer buf ByteBuffer.allocate(int size);2.创建方式2:ByteBuffer buf ByteBuffer.allocateDirect(int size); 2.字符串转成ByteBuffer的3三种方式方式1: 采用put()方法,读数据时需要调用flip()切换为读模式方式2&#xff1a;以特定编码…

ChatGPT能否撰写科研论文?

ChatGPT&#xff0c;这款被许多人誉为语言处理领域的“黑马”&#xff0c;究竟能否应用于撰写科研论文&#xff1f;近期&#xff0c;以色列理工学院生物学家兼数据科学家Roy Kishony带领的团队&#xff0c;针对这一问题进行了系列研究&#xff0c;其结果已在《Nature》杂志上发…

MySQL表的内外连接

MySQL表的内外连接 一.内连接二.外连接1. 左外连接2. 右外连接 三.案例 表的连接分为内连和外连。 一.内连接 内连接实际上就是利用where子句对两种表形成的笛卡儿积进行筛选&#xff0c;我们前面学习的查询都是内连接&#xff0c;也是在开发过程中使用的最多的连接查询。而使…

使用 Go 语言实现二叉搜索树

原文链接&#xff1a; 使用 Go 语言实现二叉搜索树 二叉树是一种常见并且非常重要的数据结构&#xff0c;在很多项目中都能看到二叉树的身影。 它有很多变种&#xff0c;比如红黑树&#xff0c;常被用作 std::map 和 std::set 的底层实现&#xff1b;B 树和 B 树&#xff0c;…

刷题笔记 day4

力扣 611 有效三角形的个数 首先需要知道如何判断 三个数是否能构成三角形。 假如 存在三个数 a < b < c&#xff0c;如果要构成三角形&#xff0c;需要满足&#xff1a; ab > c ; a c > b ; b c > a ; 任意两个数大于第三个数就可构成三角形。 其实不难…