C++容器详解

C++容器是标准模板库(STL)中的一部分,用于存储和管理数据。容器提供了多种数据结构,支持不同的用途和性能特征。常用的C++容器包括:vector、list、deque、set、map、unordered_set、unordered_map、stack、queue、priority_queue等。下面对这些容器进行详细讲解,并提供示例代码。

顺序容器

1. vector

动态数组,支持快速随机访问和在末尾高效插入和删除。

#include <iostream>
#include <vector>
using namespace std;
​
int main() {
    vector<int> vec = {1, 2, 3, 4, 5};
​
    vec.push_back(6);  // 末尾插入
    vec.pop_back();    // 末尾删除
​
    for (int i = 0; i < vec.size(); ++i) {
        cout << vec[i] << " ";
    }
    cout << endl;
​
    return 0;
}
2. list

双向链表,支持快速插入和删除,但不支持快速随机访问。

#include <iostream>
#include <list>
using namespace std;
​
int main() {
    list<int> lst = {1, 2, 3, 4, 5};
​
    lst.push_back(6);   // 末尾插入
    lst.push_front(0);  // 首部插入
    lst.pop_back();     // 末尾删除
    lst.pop_front();    // 首部删除
​
    for (auto it = lst.begin(); it != lst.end(); ++it) {
        cout << *it << " ";
    }
    cout << endl;
​
    return 0;
}
3. deque

双端队列,支持快速在两端插入和删除,支持随机访问。

#include <iostream>
#include <deque>
using namespace std;
​
int main() {
    deque<int> deq = {1, 2, 3, 4, 5};
​
    deq.push_back(6);   // 末尾插入
    deq.push_front(0);  // 首部插入
    deq.pop_back();     // 末尾删除
    deq.pop_front();    // 首部删除
​
    for (int i = 0; i < deq.size(); ++i) {
        cout << deq[i] << " ";
    }
    cout << endl;
​
    return 0;
}

关联容器

4. set

有序集合,不允许重复元素,元素按键值排序。

#include <iostream>
#include <set>
using namespace std;
​
int main() {
    set<int> s = {5, 3, 8, 1, 2};
​
    s.insert(4);    // 插入元素
    s.erase(3);     // 删除元素
​
    for (auto it = s.begin(); it != s.end(); ++it) {
        cout << *it << " ";
    }
    cout << endl;
​
    return 0;
}
5. map

有序字典,存储键值对,按键排序。

#include <iostream>
#include <map>
using namespace std;
​
int main() {
    map<int, string> m;
​
    m[1] = "one";
    m[2] = "two";
    m[3] = "three";
​
    m.insert({4, "four"});
    m.erase(2);
​
    for (auto it = m.begin(); it != m.end(); ++it) {
        cout << it->first << ": " << it->second << endl;
    }
​
    return 0;
}
6. unordered_set

无序集合,不允许重复元素,基于哈希表实现。

#include <iostream>
#include <unordered_set>
using namespace std;
​
int main() {
    unordered_set<int> us = {5, 3, 8, 1, 2};
​
    us.insert(4);   // 插入元素
    us.erase(3);    // 删除元素
​
    for (auto it = us.begin(); it != us.end(); ++it) {
        cout << *it << " ";
    }
    cout << endl;
​
    return 0;
}
7. unordered_map

无序字典,存储键值对,基于哈希表实现。

#include <iostream>
#include <unordered_map>
using namespace std;
​
int main() {
    unordered_map<int, string> um;
​
    um[1] = "one";
    um[2] = "two";
    um[3] = "three";
​
    um.insert({4, "four"});
    um.erase(2);
​
    for (auto it = um.begin(); it != um.end(); ++it) {
        cout << it->first << ": " << it->second << endl;
    }
​
    return 0;
}

容器适配器

8. stack

后进先出(LIFO)数据结构。

#include <iostream>
#include <stack>
using namespace std;
​
int main() {
    stack<int> s;
​
    s.push(1);
    s.push(2);
    s.push(3);
​
    while (!s.empty()) {
        cout << s.top() << " ";
        s.pop();
    }
    cout << endl;
​
    return 0;
}
9. queue

先进先出(FIFO)数据结构。

#include <iostream>
#include <queue>
using namespace std;

int main() {
    queue<int> q;

    q.push(1);
    q.push(2);
    q.push(3);

    while (!q.empty()) {
        cout << q.front() << " ";
        q.pop();
    }
    cout << endl;

    return 0;
}
10. priority_queue

优先级队列,默认大顶堆。

#include <iostream>
#include <queue>
using namespace std;

int main() {
    priority_queue<int> pq;

    pq.push(3);
    pq.push(1);
    pq.push(2);

    while (!pq.empty()) {
        cout << pq.top() << " ";
        pq.pop();
    }
    cout << endl;

    return 0;
}

容器的选择

  1. 快速随机访问:使用 vectordeque

  2. 快速插入和删除:使用 listdeque

  3. 需要有序性:使用 setmap

  4. 需要无序性且快速查找:使用 unordered_setunordered_map

  5. 后进先出:使用 stack

  6. 先进先出:使用 queue

  7. 优先级:使用 priority_queue

总结

C++容器是强大且多样化的数据结构库,选择合适的容器可以大大提高程序的效率和可维护性。理解每个容器的特性和适用场景是编写高效C++代码的关键。

  • 10
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值