C++ STL(标准模板库)教程
                           
天天向上
发布: 2025-03-29 17:23:01

原创
753 人浏览过

C++ 标准模板库(STL,Standard Template Library)是 C++ 提供的一套强大的数据结构和算法库,主要包含三大组件:

  1. 容器(Containers):存储数据的容器,如数组、链表、集合等。
  2. 算法(Algorithms):用于操作容器的算法,如排序、查找、修改等。
  3. 迭代器(Iterators):用于访问容器元素的工具,可以像指针一样操作容器中的元素。

C++ STL 提供了模板类和模板函数,能够使我们编写出高效且可复用的代码。接下来将介绍 C++ STL 的基本用法。


1. 容器(Containers)

STL 提供了多种容器,分为顺序容器和关联容器。

1.1 顺序容器

顺序容器用于存储有序的数据,容器中的元素按照插入顺序进行存储。常见的顺序容器有:

  • vector:动态数组。
  • list:双向链表。
  • deque:双端队列。
  • array:固定大小数组。

vector

vector 是一种动态数组容器,能够自动调整大小,支持随机访问元素。

#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {1, 2, 3, 4, 5};

    // 输出 vector 中的元素
    for (int num : v) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    // 添加元素
    v.push_back(6);
    std::cout << "After push_back(6): ";
    for (int num : v) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    // 访问元素
    std::cout << "Element at index 2: " << v[2] << std::endl;

    return 0;
}

list

list 是一个双向链表,支持高效的元素插入和删除。

#include <iostream>
#include <list>

int main() {
    std::list<int> l = {1, 2, 3, 4, 5};

    // 输出 list 中的元素
    for (int num : l) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    // 添加元素
    l.push_back(6);
    l.push_front(0);
    std::cout << "After push_back(6) and push_front(0): ";
    for (int num : l) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    return 0;
}

deque

deque 是一个双端队列,允许在两端插入和删除元素。

#include <iostream>
#include <deque>

int main() {
    std::deque<int> dq = {1, 2, 3, 4, 5};

    // 输出 deque 中的元素
    for (int num : dq) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    // 添加元素
    dq.push_back(6);
    dq.push_front(0);
    std::cout << "After push_back(6) and push_front(0): ";
    for (int num : dq) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    return 0;
}

array

array 是一个固定大小的数组容器,大小在编译时决定,适用于大小已知且固定的容器。

#include <iostream>
#include <array>

int main() {
    std::array<int, 5> arr = {1, 2, 3, 4, 5};

    // 输出 array 中的元素
    for (int num : arr) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    return 0;
}

1.2 关联容器

关联容器根据元素的键值进行存储,支持快速查找操作。常见的关联容器有:

  • set:集合,存储唯一元素。
  • map:映射,存储键值对。
  • unordered_set:无序集合。
  • unordered_map:无序映射。

set

set 是一个集合容器,自动排序并且不允许重复元素。

#include <iostream>
#include <set>

int main() {
    std::set<int> s = {5, 1, 3, 4, 2};

    // 输出 set 中的元素
    for (int num : s) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    return 0;
}

map

map 是一个键值对容器,其中的键是唯一的,按键自动排序。

#include <iostream>
#include <map>

int main() {
    std::map<int, std::string> m = {{1, "one"}, {2, "two"}, {3, "three"}};

    // 输出 map 中的键值对
    for (const auto &pair : m) {
        std::cout << pair.first << ": " << pair.second << std::endl;
    }

    return 0;
}

2. 算法(Algorithms)

STL 提供了许多用于操作容器的算法,如排序、查找、修改等。常用的算法包括:

  • sort:排序。
  • find:查找元素。
  • reverse:反转容器。
  • accumulate:累加容器中的元素。

2.1 sort

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> v = {5, 3, 4, 1, 2};

    // 排序
    std::sort(v.begin(), v.end());

    // 输出排序后的元素
    for (int num : v) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    return 0;
}

2.2 find

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> v = {5, 3, 4, 1, 2};

    // 查找元素
    auto it = std::find(v.begin(), v.end(), 4);
    if (it != v.end()) {
        std::cout << "Element found: " << *it << std::endl;
    } else {
        std::cout << "Element not found!" << std::endl;
    }

    return 0;
}

2.3 reverse

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> v = {1, 2, 3, 4, 5};

    // 反转容器
    std::reverse(v.begin(), v.end());

    // 输出反转后的元素
    for (int num : v) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    return 0;
}

3. 迭代器(Iterators)

STL 的迭代器类似于指针,允许我们遍历容器中的元素。常见的迭代器操作有:

  • begin():返回容器的第一个元素的迭代器。
  • end():返回容器的最后一个元素的下一个位置的迭代器。
  • rbegin():返回容器的反向迭代器,指向最后一个元素。
  • rend():返回容器的反向迭代器,指向第一个元素的前一个位置。

3.1 使用迭代器遍历容器

#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {1, 2, 3, 4, 5};

    // 使用迭代器遍历容器
    for (auto it = v.begin(); it != v.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    return 0;
}

4. 总结

STL 提供了强大的容器、算法和迭代器,极大地简化了 C++ 编程。通过使用 STL,可以更高效地实现常见的数据结构操作和算法。掌握 STL 是 C++ 编程的重要基础。

发表回复 0

Your email address will not be published. Required fields are marked *