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

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:表示一组唯一的向量。

http://www.kler.cn/a/595581.html

相关文章:

  • 前端如何请求后端服务?---------nignx
  • Windows 图形显示驱动开发-WDDM 2.9功能- 支持跨适配器资源扫描 (CASO)(一)
  • 传感器研习社:Swift Navigation与意法半导体(STMicroelectronics)合作 共同推出端到端GNSS汽车自动驾驶解决方案
  • ES、Kibana一键式部署脚本执行文件,外加IK分词器和拼音分词器
  • Flink SQL 技术原理详解
  • 使用 Google Firebase 控制台和 ESP8266 NodeMCU 的物联网控制 LED
  • JavaScript实现一个函数,将数组扁平化(flatten),即把多维数组转为一维数组。
  • Visual Studio Code 连接 SAP ERP 系统
  • SpringBoot实现异步调用的方法
  • 北斗导航 | 北斗三号区域短报文相关知识总结
  • 一份针对零基础学习AI Agent详细学习计划
  • 【Ratis】Ratis Streaming概览
  • numpy学习笔记13:np.random.choice和np.cumsum的解释
  • Docker 速通(总结)
  • 【Unity基础】Unity中角色动画的三种实现方式
  • DAY13 线程池、死锁、线程状态、计时器
  • 如何在ubuntu上安装zookeeper
  • 在 Elasticsearch 中探索基于 NVIDIA 的 GPU 加速向量搜索
  • w265基于Spring Boot库存管理系统
  • 【C++】static、内部类