C++相关数据结构的API调用

发布于:2025-03-16 ⋅ 阅读:(14) ⋅ 点赞:(0)

接下来分别展示动态数组,链表,队列,栈,哈希表的相关代码。由于之前用的C语言,对于实现相关数据结构需要自己手动去做,比较麻烦,现在学习了C++关于数据结构的一些基础操作,来简便刷题的过程。下面我把完整的程序切割成几份来分别展示:

动态数组(vector)

#include <bits/stdc++.h>
using namespace std;

int main()
{
    //动态数组:
    int n = 5;
    //数组大小为5的动态数组
    vector<int> nums(n);

    cout<<nums.empty()<<endl;
    //数组的大小
    cout<<nums.size()<<endl;

    //在尾部插入元素
    nums.push_back(666);
    cout<<nums.size()<<endl;

    //得到数组的最后一个元素
    cout<<nums.back()<<endl;

    //删除最后一个元素
    nums.pop_back();
    cout<<nums.size()<<endl;

    //可以通过方括号直接进行修改
    nums[1] = 222;
    cout<<nums[1]<<endl;

    //在索引为3的地方加入元素
    nums.insert(nums.begin()+3,100);

    //删除索引为2的元素
    nums.erase(nums.begin()+2);

    //交换两个元素
    swap(nums[0],nums[1]);


    for(int i = 0;i < nums.size();i++)
    {
        cout<<nums[i]<<" ";
    }

链表(list)

    //链表:
    // 初始化链表
    list<int> lst{1, 2, 3, 4, 5};

    // 检查链表是否为空,输出:false
    cout << lst.empty() << endl;

    // 获取链表的大小,输出:5
    cout << lst.size() << endl;

    // 在链表头部插入元素 0
    lst.push_front(0);
    // 在链表尾部插入元素 6
    lst.push_back(6);

    // 获取链表头部和尾部元素,输出:0 6
    cout << lst.front() << " " << lst.back() << endl;

    // 删除链表头部元素
    lst.pop_front();
    // 删除链表尾部元素
    lst.pop_back();

    // 在链表中插入元素
    auto it = lst.begin();
    // 移动到第三个位置
    advance(it, 2);
    // 在第三个位置插入 99
    lst.insert(it, 99);

    // 删除链表中某个元素
    it = lst.begin();
    // 移动到第二个位置
    advance(it, 1);
    // 删除第二个位置的元素
    lst.erase(it);

    // 遍历链表
    // 输出:1 99 3 4 5
    for (int val : lst) {
        cout << val << " ";
    }
    cout << endl;

队列(queue)

    // 初始化一个空的整型队列 q
    queue<int> q;

    // 在队尾添加元素
    q.push(10);
    q.push(20);
    q.push(30);

    // 检查队列是否为空,输出:false
    cout << q.empty() << endl;

    // 获取队列的大小,输出:3
    cout << q.size() << endl;

    // 获取队列的队头和队尾元素,输出:10 和 30
    cout << q.front() << " " << q.back() << endl;

    // 删除队头元素
    q.pop();

    // 输出新的队头元素:20
    cout << q.front() << endl;

栈(stack)

    // 初始化一个空的整型栈 s
    stack<int> s;

    // 向栈顶添加元素
    s.push(10);
    s.push(20);
    s.push(30);

    // 检查栈是否为空,输出:false
    cout << s.empty() << endl;

    // 获取栈的大小,输出:3
    cout << s.size() << endl;

    // 获取栈顶元素,输出:30
    cout << s.top() << endl;

    // 删除栈顶元素
    s.pop();

    // 输出新的栈顶元素:20
    cout << s.top() << endl;

哈希表(unordered_map)


    // 初始化哈希表
    unordered_map<int, string> hashmap{{1, "one"}, {2, "two"}, {3, "three"}};

    // 检查哈希表是否为空,输出:0 (false)
    cout << hashmap.empty() << endl;

    // 获取哈希表的大小,输出:3
    cout << hashmap.size() << endl;


    // 获取指定键对应的值,若不存在会返回默认构造的值
    // 输出空字符串
    cout << hashmap[4] << endl;

    // 插入一个新的键值对
    hashmap[4] = "four";

    // 获取新插入的值,输出:four
    cout << hashmap[4] << endl;

    // 删除键值对
    hashmap.erase(3);


    // 遍历哈希表
    // 输出(顺序可能不同):
    // 4 -> four
    // 2 -> two
    // 1 -> one
    for (const auto &pair: hashmap) {
        cout << pair.first << " -> " << pair.second << endl;
    }

    // 特别注意,访问不存在的键会自动创建这个键
    unordered_map<int, string> hashmap2;

    // 键值对的数量是 0
    cout << hashmap2.size() << endl; // 0

    // 访问不存在的键,会自动创建这个键,对应的值是默认构造的值
    cout << hashmap2[1] << endl; // empty string
    cout << hashmap2[2] << endl; // empty string

    // 现在键值对的数量是 2
    cout << hashmap2.size() << endl; // 2

}