当前位置: 首页 > news >正文

C++的常用容器嵌套

  在 C++ 中,数据结构之间的嵌套是非常常见的,尤其是在处理复杂数据时。以下是几种最常用的数据结构嵌套方式及其典型应用场景的总结:


1. std::vector 嵌套 std::vector

  • 定义std::vector<std::vector<T>>
  • 用途:表示二维数组或矩阵。
  • 示例
    #include <iostream>
    #include <vector>
    
    int main() {
        std::vector<std::vector<int>> matrix = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };
    
        // 遍历二维 vector
        for (const auto& row : matrix) {
            for (int value : row) {
                std::cout << value << " ";
            }
            std::cout << std::endl;
        }
    
        return 0;
    }
    
  • 输出
    1 2 3 
    4 5 6 
    7 8 9 
    

2. std::map 嵌套 std::vector

  • 定义std::map<Key, std::vector<T>>
  • 用途:表示键到一组值的映射(例如,一个班级中每个学生的成绩列表)。
  • 示例
    #include <iostream>
    #include <map>
    #include <vector>
    
    int main() {
        std::map<std::string, std::vector<int>> studentGrades = {
            {"Alice", {90, 85, 88}},
            {"Bob", {78, 82, 80}},
            {"Charlie", {95, 91, 89}}
        };
    
        // 遍历 map 中的 vector
        for (const auto& [name, grades] : studentGrades) {
            std::cout << name << ": ";
            for (int grade : grades) {
                std::cout << grade << " ";
            }
            std::cout << std::endl;
        }
    
        return 0;
    }
    
  • 输出
    Alice: 90 85 88 
    Bob: 78 82 80 
    Charlie: 95 91 89 
    

3. std::vector 嵌套 std::map

  • 定义std::vector<std::map<Key, Value>>
  • 用途:表示一组键值对集合(例如,多个学生的属性集合)。
  • 示例
    #include <iostream>
    #include <vector>
    #include <map>
    
    int main() {
        std::vector<std::map<std::string, std::string>> students = {
            {{"name", "Alice"}, {"age", "20"}},
            {{"name", "Bob"}, {"age", "21"}},
            {{"name", "Charlie"}, {"age", "22"}}
        };
    
        // 遍历 vector 中的 map
        for (const auto& student : students) {
            for (const auto& [key, value] : student) {
                std::cout << key << ": " << value << " ";
            }
            std::cout << std::endl;
        }
    
        return 0;
    }
    
  • 输出
    name: Alice age: 20 
    name: Bob age: 21 
    name: Charlie age: 22 
    

4. std::map 嵌套 std::map

  • 定义std::map<Key1, std::map<Key2, Value>>
  • 用途:表示多层键值对映射(例如,城市到区域到人口数量的映射)。
  • 示例
    #include <iostream>
    #include <map>
    
    int main() {
        std::map<std::string, std::map<std::string, int>> cityPopulation = {
            {"New York", {{"Manhattan", 1600000}, {"Brooklyn", 2600000}}},
            {"Los Angeles", {{"Downtown", 500000}, {"Hollywood", 200000}}}
        };
    
        // 遍历嵌套的 map
        for (const auto& [city, areas] : cityPopulation) {
            std::cout << city << ":\n";
            for (const auto& [area, population] : areas) {
                std::cout << "  " << area << ": " << population << "\n";
            }
        }
    
        return 0;
    }
    
  • 输出
    New York:
      Manhattan: 1600000
      Brooklyn: 2600000
    Los Angeles:
      Downtown: 500000
      Hollywood: 200000
    

5. std::vector 嵌套 std::pair

  • 定义std::vector<std::pair<T1, T2>>
  • 用途:表示一组键值对(例如,存储多个学生的姓名和年龄)。
  • 示例
    #include <iostream>
    #include <vector>
    #include <utility>  // 包含 std::pair
    
    int main() {
        std::vector<std::pair<std::string, int>> students = {
            {"Alice", 20},
            {"Bob", 21},
            {"Charlie", 22}
        };
    
        // 遍历 vector 中的 pair
        for (const auto& [name, age] : students) {
            std::cout << name << ": " << age << std::endl;
        }
    
        return 0;
    }
    
  • 输出
    Alice: 20
    Bob: 21
    Charlie: 22
    

6. std::map 嵌套 std::pair

  • 定义std::map<Key, std::pair<T1, T2>>
  • 用途:表示键到一对值的映射(例如,学生姓名到年龄和成绩的映射)。
  • 示例
    #include <iostream>
    #include <map>
    #include <utility>  // 包含 std::pair
    
    int main() {
        std::map<std::string, std::pair<int, int>> studentInfo = {
            {"Alice", {20, 90}},
            {"Bob", {21, 85}},
            {"Charlie", {22, 95}}
        };
    
        // 遍历 map 中的 pair
        for (const auto& [name, info] : studentInfo) {
            std::cout << name << ": Age = " << info.first << ", Grade = " << info.second << std::endl;
        }
    
        return 0;
    }
    
  • 输出
    Alice: Age = 20, Grade = 90
    Bob: Age = 21, Grade = 85
    Charlie: Age = 22, Grade = 95
    

7. std::set 嵌套 std::vector

  • 定义std::set<std::vector<T>>
  • 用途:表示一组唯一的向量(例如,存储唯一的路径或组合)。
  • 示例
    #include <iostream>
    #include <set>
    #include <vector>
    
    int main() {
        std::set<std::vector<int>> uniqueVectors = {
            {1, 2, 3},
            {4, 5, 6},
            {1, 2, 3}  // 重复,不会被插入
        };
    
        // 遍历 set 中的 vector
        for (const auto& vec : uniqueVectors) {
            for (int value : vec) {
                std::cout << value << " ";
            }
            std::cout << std::endl;
        }
    
        return 0;
    }
    
  • 输出
    1 2 3 
    4 5 6 
    

总结

  • std::vector 嵌套 std::vector:表示二维数组或矩阵。
  • std::map 嵌套 std::vector:表示键到一组值的映射。
  • std::vector 嵌套 std::map:表示一组键值对集合。
  • std::map 嵌套 std::map:表示多层键值对映射。
  • std::vector 嵌套 std::pair:表示一组键值对。
  • std::map 嵌套 std::pair:表示键到一对值的映射。
  • std::set 嵌套 std::vector:表示一组唯一的向量。

相关文章:

  • Android Compose 基础布局之 Box 和 Stack 源码深度剖析(九)
  • 【留一下记录】Vllm在Linux环境下的学习笔记
  • 多路FM调频广播解调器:多路电台FM广播信号一体化解调处理方案
  • Burp Suite HTTPS解密原理
  • 星越L_大灯延时关闭使用讲解
  • vue3之写一个aichat---实现聊天逻辑
  • OpenCV Imgproc 模块使用指南(Python 版)
  • 【ACM竞赛的必要性】
  • 鸿蒙开发工程师简历项目撰写全攻略
  • 力扣刷题——143.重排链表
  • 如何利用环境监控看板提升工厂生产质量和效率
  • 《信息系统安全》(第一次上机实验报告)
  • 无需邀请码,实在智能发布通用智能体-实在Agent
  • 智能,触手可及:揭秘高灵活、高精度仿生机器手的操作与实现
  • django怎么配置404和500
  • 【PCIe 总线及设备入门学习专栏 3.1 -- PCIe 中为何只有 TLP 会被 Switch 和 RC 进行路由?】
  • 【实操】Mybatis-plus2.x升级到3.x
  • 基于深度学习的运动想象脑电信号分类研究
  • django如何配置使用asgi
  • LDAP从入门到实战:环境部署与配置指南(上)
  • 第三届“老山国际春茶节”活动在云南麻栗坡举办
  • 巴基斯坦军方:印度袭击已致巴方31人死亡
  • 六大车企一季报:比亚迪近92亿净利稳居第一,多家车企营收下滑
  • 微软通讯软件Skype正式停止运营:斥资85亿美元购入,月活用户曾超3亿
  • 实探北京楼市:“好房子”卖点十足,二手房持续回稳
  • 五一小长假上海“人从众”,全要素旅游交易总额超200亿元