为企业做网站策划案电商seo是指
在 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}};// 遍历二维 vectorfor (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 中的 vectorfor (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 中的 mapfor (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}}}};// 遍历嵌套的 mapfor (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: 1600000Brooklyn: 2600000 Los Angeles:Downtown: 500000Hollywood: 200000
5. std::vector
嵌套 std::pair
- 定义:
std::vector<std::pair<T1, T2>>
。 - 用途:表示一组键值对(例如,存储多个学生的姓名和年龄)。
- 示例:
#include <iostream> #include <vector> #include <utility> // 包含 std::pairint main() {std::vector<std::pair<std::string, int>> students = {{"Alice", 20},{"Bob", 21},{"Charlie", 22}};// 遍历 vector 中的 pairfor (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::pairint main() {std::map<std::string, std::pair<int, int>> studentInfo = {{"Alice", {20, 90}},{"Bob", {21, 85}},{"Charlie", {22, 95}}};// 遍历 map 中的 pairfor (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 中的 vectorfor (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
:表示一组唯一的向量。