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;
}
相关推荐
嵌入式小李.man1 分钟前
linux中多路复用IO:select、poll和epoll
linux·c++
郝学胜-神的一滴5 分钟前
QAxios研发笔记(二):在Qt环境下基于Promise风格简化Http的Post请求
开发语言·c++·笔记·qt·网络协议·程序人生·http
敲代码的嘎仔8 分钟前
数据结构算法学习day3——二分查找
java·开发语言·数据结构·学习·程序人生·算法·职场和发展
大白的编程日记.12 分钟前
【Linux学习笔记】线程安全问题之单例模式和死锁
linux·笔记·学习
m5655bj26 分钟前
如何使用 Python 转换 Excel 工作表到 PDF 文档
开发语言·c#·excel
晨非辰31 分钟前
《数据结构风云》:二叉树遍历的底层思维>递归与迭代的双重视角
数据结构·c++·人工智能·算法·链表·面试
JJJJ_iii33 分钟前
【机器学习12】无监督学习:K-均值聚类与异常检测
人工智能·笔记·python·学习·机器学习·均值算法·聚类
ᐇ95943 分钟前
Java核心概念深度解析:从包装类到泛型的全面指南
java·开发语言
逻极1 小时前
Rust之旅的起点:为什么选择Rust?
开发语言·后端·rust
Tony Bai1 小时前
从 Python 到 Go:我们失去了什么,又得到了什么?
开发语言·后端·python·golang