0
点赞
收藏
分享

微信扫一扫

C++中STL的详细讲解

C++标准模板库(Standard Template Library,STL)是C++标准库的一部分,它提供了一组通用的、可重用的模板类和函数,旨在简化数据结构和算法的实现。STL主要由以下几个部分组成:

  1. 容器(Containers):用于存储数据的对象。
  2. 算法(Algorithms):对容器中的数据进行操作的函数。
  3. 迭代器(Iterators):用于遍历容器中的元素的对象。
  4. 仿函数(Functors):可以像函数一样被调用的对象。

接下来,我们将详细讨论这几个部分,并通过示例代码进一步解释它们的用法。

一、容器(Containers)

STL 提供了多种类型的容器,每种容器都有其特定的使用场景。常见的容器包括:

1. 序列容器(Sequence Containers)

序列容器保持元素的线性顺序。

  • vector
  • 动态数组,提供快速随机访问。
  • 尺寸可动态调整。

#include <iostream>  
#include <vector>  

int main() {  
    std::vector<int> vec = {1, 2, 3, 4, 5};  
    vec.push_back(6); // 添加元素  
    for (int num : vec) {  
        std::cout << num << " "; // 输出:1 2 3 4 5 6  
    }  
    return 0;  
}

  • list
  • 双向链表,适用于频繁插入和删除的情况。

#include <iostream>  
#include <list>  

int main() {  
    std::list<int> lst = {1, 2, 3, 4};  
    lst.push_back(5); // 尾部插入  
    lst.push_front(0); // 头部插入  
    for (int num : lst) {  
        std::cout << num << " "; // 输出:0 1 2 3 4 5  
    }  
    return 0;  
}

  • deque
  • 双端队列,可以在两端快速插入和删除元素。

#include <iostream>  
#include <deque>  

int main() {  
    std::deque<int> deq = {1, 2, 3};  
    deq.push_front(0); // 头部插入  
    deq.push_back(4);  // 尾部插入  
    for (int num : deq) {  
        std::cout << num << " "; // 输出:0 1 2 3 4  
    }  
    return 0;  
}

2. 关联容器(Associative Containers)

关联容器根据键值进行组织和查找。

  • set
  • 存储唯一元素,并根据键自动排序。

#include <iostream>  
#include <set>  

int main() {  
    std::set<int> s = {3, 1, 4, 2};  
    s.insert(5); // 插入元素  
    for (int num : s) {  
        std::cout << num << " "; // 输出:1 2 3 4 5  
    }  
    return 0;  
}

  • map
  • 存储键值对,键唯一,自动排序。

#include <iostream>  
#include <map>  

int main() {  
    std::map<std::string, int> ages = {  
        {"Alice", 30},  
        {"Bob", 25}  
    };  
    ages["Charlie"] = 35; // 插入或更新  
    for (const auto& pair : ages) {  
        std::cout << pair.first << ": " << pair.second << std::endl; // 输出:Alice: 30 Bob: 25 Charlie: 35  
    }  
    return 0;  
}

3. 无序容器(Unordered Containers)

无序容器使用哈希表来存储元素,并提供快速的查找。

  • unordered_set
  • 存储唯一元素,但不保持顺序。

#include <iostream>  
#include <unordered_set>  

int main() {  
    std::unordered_set<int> us = {3, 1, 4, 2};  
    us.insert(5);  
    for (int num : us) {  
        std::cout << num << " "; // 输出顺序不定  
    }  
    return 0;  
}

  • unordered_map
  • 存储键值对,但不保持键的顺序。

#include <iostream>  
#include <unordered_map>  

int main() {  
    std::unordered_map<std::string, int> ages = {  
        {"Alice", 30},  
        {"Bob", 25}  
    };  
    ages["Charlie"] = 35;  
    for (const auto& pair : ages) {  
        std::cout << pair.first << ": " << pair.second << std::endl; // 输出顺序不定  
    }  
    return 0;  
}

二、迭代器(Iterators)

迭代器是用于遍历容器中元素的对象,可以看作是指向容器元素的指针。STL 提供了多种类型的迭代器。

1. 基本使用

#include <iostream>  
#include <vector>  

int main() {  
    std::vector<int> vec = {1, 2, 3, 4, 5};  
    for (std::vector<int>::iterator it = vec.begin(); it != vec.end(); ++it) {  
        std::cout << *it << " "; // 输出:1 2 3 4 5  
    }  
    return 0;  
}

2. 范围-based for 循环

C++11 引入了范围-based for 循环,使得遍历容器更加简洁:

#include <iostream>  
#include <vector>  

int main() {  
    std::vector<int> vec = {1, 2, 3, 4, 5};  
    for (int num : vec) {  
        std::cout << num << " "; // 输出:1 2 3 4 5  
    }  
    return 0;  
}

三、算法(Algorithms)

STL 提供了多种常用的算法,这些算法可以直接应用于容器,使用迭代器作为参数。

1. 排序(Sort)

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

int main() {  
    std::vector<int> vec = {5, 3, 4, 1, 2};  
    std::sort(vec.begin(), vec.end()); // 按升序排序  
    for (int num : vec) {  
        std::cout << num << " "; // 输出:1 2 3 4 5  
    }  
    return 0;  
}

2. 查找(Find)

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

int main() {  
    std::vector<int> vec = {1, 2, 3, 4, 5};  
    auto it = std::find(vec.begin(), vec.end(), 3); // 查找值为3的元素  
    if (it != vec.end()) {  
        std::cout << "Found: " << *it << std::endl; // 输出:Found: 3  
    }  
    return 0;  
}

四、仿函数(Functors)

仿函数是可以像普通函数一样被调用的对象。C++ 中,仿函数通常通过重载 operator() 来实现。

#include <iostream>  

class Add {  
public:  
    int operator()(int a, int b) {  
        return a + b;  
    }  
};  

int main() {  
    Add add;  
    std::cout << add(2, 3) << std::endl; // 输出:5  
    return 0;  
}

仿函数在 STL 中非常有用,尤其是在使用算法时,可以直接传递仿函数实例作为参数

举报

相关推荐

0 条评论