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

前置:

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

一.string--字符串

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

命名空间:std

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

代码运用:


#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

使用演示:

#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排序算法

代码演示:

#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;
}
  • 7
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

初猿°

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值