C++笔记:STL容器库的使用

前置:

对于stl容器库,我只做了一些常用的笔记,关于更详细的使用可以参考:https://cppreference.com/https://cppreference.com/

一.string--字符串

对于C++中string字符串会比C语言的字符数组使用起来会顺手许多。

命名空间:std

关于迭代器可以理解为指针,和指针的使用方法差不多,对于迭代器更细的描述会在后续的笔记中更新。

代码运用:

cpp 复制代码
#include <iostream>
#include <string>
using namespace std;

int main() {
    //定义一个字符串,并初始化
    string str = "nihao nihao";
    cout << "str = " << str << endl;
    //访问位置1的字符并赋值给字符c
    char c = str[1];
    cout << "c = " << c << endl;
    string str2 = "hello";
    //使用等号进行字符串拷贝赋值
    str = str2;
    cout << "str = " << str << endl;
    str += " world";
    cout << "str = " << str << endl;
    cout << "str len = " << str.length() << endl;
    cout << "str is emtpy " << boolalpha << str.empty() << endl;
    int ind = 4;
    cout << "str[" << ind << "] = " << str.at(ind) << endl;
    cout << "str.front() = " << str.front() << endl;
    cout << "str.back() = " << str.back() << endl;
    cout << "str.find(l) = " << str.find("l") << endl;
    cout << "str.rfind(l) = " << str.rfind("l") << endl;
    //在字符串最后的位置加上" nihao"
    str.insert(str.length() - 1, " nihao");
    cout << "str = " << str << endl;
    //将刚刚插入的" nihao"删除
    str.erase(str.length() - 7, 6);
    cout << "str = " << str << endl;
    //从0位置开始替换6个字符,并替换为ni hao
    str.replace(0, 6, "ni hao");
    cout << "str = " << str << endl;
    return 0;
}

二.vector容器

C语言中使用数组的方法都可以替换到vector中进行使用。

命名空间:std

三.queue容器

对于queue容器,他就是数据结构中的队列。

命名空间:std

注意:queue容器使用方式要像队列的方式去使用。

成员方法只是常用的举例出来了,还有其他的。

priority_queue容器

四.stack容器

对于stack容器,他就是数据结构中的栈。

注意:stack容器使用方式要像栈的方式去使用。

命名空间:std

五.set容器

set 是 C++ 标准库中的关联容器之一,它提供了一种存储唯一元素的方式,并且以红黑树作为底层实现。

命名空间:std

使用演示:

cpp 复制代码
#include <iostream>
#include <set>
#include <ctime>
using namespace std;

void output(set<int> s) {
    //用set<int>迭代器对象进行定义 i对象,然后进行遍历set里的元素
    for (set<int>::iterator i = s.begin(); i != s.end(); i++) {
        i != s.begin() && cout << " ";
        //把迭代器看为指针,那么要对指针进行取值就是利用*
        cout << *i;
    }
    cout << endl;
    return ;
}

int main() {
    srand(time(0));
    set<int> s;
    for (int i = 0; i < 10; i++) {
        i && cout << " ";
        int val = rand() % 100;
        //添加元素进s
        s.insert(val);
        cout << val;
    }
    cout << endl;
    //通过输出发现,set将插入进的元素进行了排序
    output(s);
    for (int i = 0; i < 10; i++) {
        int val = rand() % 100;
        //s.find返回是迭代器,理解为指针
        //如果没有找到他就返回一个指针指向最后一个元素的后一个位置,也就是s.end();
        //那么没有找到就返回是s.end()的位置,那就通过这样去判断找没找到
        if (s.find(val) == s.end()) {
            cout << val << "is not find in set" << endl;
            continue;
        }
        cout << "find " << val << " in set" << endl;
        //找到了并删除
        cout << "erase" << val << " at set" << endl;
        s.erase(val);
    }
    return 0;
}

unordered_set

**区别:**set他会将,元素进行排序,而unordered_set不会将元素进行排序,其他使用方式都相同。头文件<unordered_set>

六.map容器

map是 C++ STL 中的关联容器,用于存储键值对。它提供了一种将键映射到值的机制,其中每个键必须是唯一的.

命名空间:std

七.sort排序算法

代码演示:

cpp 复制代码
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <algorithm>
using namespace std;

void output(vector<int> arr) {
    for (int i = 0; i < 10; i++) {
        i && cout << " ";
        cout << arr[i];
    }
    cout << endl;
    return ;
}

bool comp(int a, int b) {
    return a > b;
}

int main() {
    srand(time(0));
    vector<int> arr;
    for (int i = 0; i < 10; i++) {
        arr.push_back(rand() % 100);
    }
    output(arr);
    sort(arr.begin(), arr.end());
    output(arr);
    sort(arr.begin(), arr.end(), comp);
    output(arr);
    return 0;
}
相关推荐
会周易的程序员6 小时前
openplc runtimev4 Docker 部署
运维·c++·物联网·docker·容器·软件工程·iot
難釋懷6 小时前
Lua脚本解决多条命令原子性问题
开发语言·lua
爱装代码的小瓶子6 小时前
【C++与Linux基础】进程间通讯方式:匿名管道
android·c++·后端
CoderCodingNo6 小时前
【GESP】C++ 二级真题解析,[2025年12月]第一题环保能量球
开发语言·c++·算法
独好紫罗兰6 小时前
对python的再认识-基于数据结构进行-a005-元组-CRUD
开发语言·数据结构·python
LYOBOYI1236 小时前
qtcpSocket详解
c++·qt
REDcker6 小时前
gRPC完整文档
服务器·网络·c++·网络协议·grpc
chilavert3186 小时前
技术演进中的开发沉思-356:重排序(中)
java·开发语言
devmoon6 小时前
为 Pallet 搭建最小化 Mock Runtime 并编写单元测试环境
开发语言·单元测试·区块链·智能合约·polkadot
听麟6 小时前
HarmonyOS 6.0+ PC端虚拟仿真训练系统开发实战:3D引擎集成与交互联动落地
笔记·深度学习·3d·华为·交互·harmonyos