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;
}
相关推荐
蟹至之12 分钟前
【Java】异常的初步认识
java·开发语言·类和对象·异常
无垠的广袤16 分钟前
【萤火工场GD32VW553-IOT开发板】流水灯
c++·单片机·嵌入式硬件·物联网
佩奇的技术笔记21 分钟前
Python入门手册:Python基础语法
开发语言·python
学习使我变快乐29 分钟前
C++:无序容器
数据结构·c++·算法
IT英语写作研习社1 小时前
英语写作中“假设”suppose, assume, presume 的用法
笔记
学习使我变快乐1 小时前
C++:STL
开发语言·c++
whoarethenext2 小时前
C/C++的OpenCV 进行轮廓提取
c语言·c++·opencv·轮廓提取
m0_689618282 小时前
3D打印仿造+ AI大脑赋能,造出会思考的全景相机
笔记·科技·数码相机·3d
PingdiGuo_guo2 小时前
C++指针(二)
开发语言·c++
Magnetic_h2 小时前
【iOS】类结构分析
开发语言·笔记·学习·ios·objective-c