C++中STL容器应用

简介: C++中STL容器应用

一、STL容器核心应用

1. vector(动态数组)

特性:连续内存、随机访问O(1)、尾部操作高效

// 高性能数值计算
vector<double> processSensorData() {
   
    vector<double> readings;
    readings.reserve(1000);  // 预分配内存避免多次扩容

    for(int i=0; i<1000; ++i) {
   
        readings.push_back(getSensorValue());
    }

    // 使用STL算法处理数据
    auto max_val = *max_element(readings.begin(), readings.end());
    transform(readings.begin(), readings.end(), readings.begin(),
             [max_val](double x) {
    return x/max_val; });

    return readings;
}

// 二维数组模拟
vector<vector<int>> matrix(5, vector<int>(5, 0));  // 5x5矩阵初始化
AI 代码解读

2. list(双向链表)

特性:任意位置插入/删除O(1)、不支持随机访问

// 高频交易订单簿
struct Order {
   
    double price;
    int quantity;
};

list<Order> orderBook;

// 插入时自动排序
void addOrder(Order newOrder) {
   
    auto it = find_if(orderBook.begin(), orderBook.end(),
                     [&](const Order& o) {
    return o.price > newOrder.price; });
    orderBook.insert(it, newOrder);
}

// 快速删除失效订单
orderBook.remove_if([](const Order& o) {
    return o.quantity <= 0; });
AI 代码解读

3. map/set(红黑树实现)

特性:自动排序、查找O(log n)

// 字典管理
map<string, string> configMap = {
   
    {
   "resolution", "1920x1080"},
    {
   "volume", "75"}
};

// 安全访问
if(auto it = configMap.find("volume"); it != configMap.end()) {
   
    cout << "当前音量:" << it->second << endl;
}

// 多键索引
multimap<string, Employee> nameIndex;  // 允许重复键
AI 代码解读

4. unordered_map(哈希表)

特性:平均O(1)访问、无序存储

// 快速查找表
unordered_map<string, int> wordCount;

// 词频统计
string text = "a quick brown fox jumps over the lazy dog";
istringstream iss(text);
string word;
while(iss >> word) {
   
    ++wordCount[word];
}

// 查找高频词
auto highFreq = max_element(wordCount.begin(), wordCount.end(),
                           [](auto& a, auto& b) {
    return a.second < b.second; });
AI 代码解读

5. deque(双端队列)

特性:头尾操作高效、分段连续存储

// 滑动窗口处理
deque<int> window;
const int WINDOW_SIZE = 5;

void processData(int newValue) {
   
    window.push_back(newValue);
    if(window.size() > WINDOW_SIZE) {
   
        window.pop_front();
    }

    // 计算窗口平均值
    double avg = accumulate(window.begin(), window.end(), 0.0) / window.size();
}
AI 代码解读

常见陷阱与解决方案

  1. 迭代器失效问题
    vector<int> vec {
         1,2,3,4,5};
    for(auto it = vec.begin(); it != vec.end(); ) {
         
     if(*it % 2 == 0) {
         
         it = vec.erase(it);  // 正确方式
     } else {
         
         ++it;
     }
    }
    
    AI 代码解读
目录
相关文章
|
10月前
|
Lua语法(一)
Lua语法(一)
220 0
Lua语法(一)
|
10月前
|
Linux运行时常用命令
Linux运行时常用命令
146 0
Linux运行时常用命令
|
10月前
|
Git常用命令的详细指南
Git常用命令的详细指南
313 0
Git常用命令的详细指南
|
10月前
|
Lua语法(五)——垃圾回收
Lua语法(五)——垃圾回收
198 0
|
10月前
|
makefile 函数全解
makefile 函数全解
764 0
makefile 函数全解
|
10月前
|
C++多线程相关应用
C++多线程相关应用
96 0
C++多线程相关应用
|
10月前
|
C++中`std::function`和`std::bind`的详细解析
C++中`std::function`和`std::bind`的详细解析
105 0
C++中`std::function`和`std::bind`的详细解析
|
10月前
|
红黑树解析与应用
红黑树解析与应用
136 0
红黑树解析与应用
【C语言】B树和B+树的解析应用
【C语言】B树和B+树的解析应用
73 0
【C语言】B树和B+树的解析应用
|
10月前
|
Hash与布隆过滤器
Hash与布隆过滤器
100 0
Hash与布隆过滤器
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等