如何使用递归迭代嵌套映射中的映射?

huangapple go评论99阅读模式
英文:

How to iterate through a map of nested maps using recursion?

问题

我有嵌套的映射,需要遍历每个级别以打印所有的值。

这个映射是为了向您展示我正在尝试使用"递归"解决的问题的示例。

  1. MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow) {
  2. ui->setupUi(this);
  3. std::map<std::string, VariableContainer> myMap;
  4. for (int i = 0; i < 3; ++i) {
  5. VariableContainer var;
  6. var.variables["A" + std::to_string(i)];
  7. if (i == 1) {
  8. VariableContainer childVar;
  9. for (int j = 1; j < 3; ++j) {
  10. childVar.variables["X_" + std::to_string(j)];
  11. }
  12. var.variables.emplace("B" + std::to_string(i), childVar);
  13. }
  14. var.variables["C" + std::to_string(i)];
  15. myMap.emplace("Node_" + std::to_string(i), var);
  16. }
  17. for (auto& var : myMap) {
  18. std::cout << "var: " << var.first << std::endl;
  19. if (var.second.variables.size() > 0) {
  20. for (auto& child : var.second.variables) {
  21. std::cout << " child: " << child.first << std::endl;
  22. if (child.second.variables.size() > 0) {
  23. for (auto& subChild : child.second.variables) {
  24. std::cout << " subChild: " << subChild.first << std::endl;
  25. }
  26. }
  27. }
  28. }
  29. }
  30. }

这是输出:

  1. var: Node_0
  2. child: A0
  3. child: C0
  4. var: Node_1
  5. child: A1
  6. child: B1
  7. subChild: X_1
  8. subChild: X_2
  9. child: C1
  10. var: Node_2
  11. child: A2
  12. child: C2

但我需要以以下结构输出:node + "." + child + "." + subChild + "." + nsubChilds

  1. Node_0.A0
  2. Node_0.C0
  3. Node_1.A1
  4. Node_1.B1.X_1
  5. Node_1.B1.X_2
  6. Node_1.C1
  7. Node_2.A2
  8. Node_2.C2

这是 VariableContainer.h

  1. #ifndef VARIABLECONTAINER_H
  2. #define VARIABLECONTAINER_H
  3. #include <map>
  4. #include <string>
  5. class VariableContainer
  6. {
  7. public:
  8. VariableContainer() {}
  9. std::map<std::string, VariableContainer> variables;
  10. };
  11. #endif // VARIABLECONTAINER_H

我可以在其他映射中嵌套映射,类似于分支。我认为使用递归是一个好的解决方案,因为每次执行程序时嵌套映射的数量都不同。

例如,我可能有类似这样的内容:Node_1.B1.X_1.Y_1.Z_1

我尝试了以下递归函数,但没有获得期望的输出。

  1. void MainWindow::showMap(std::map<std::string, VariableContainer> &map) {
  2. for (auto& var : map) {
  3. std::cout << "var: " << var.first << std::endl;
  4. if (var.second.variables.size() > 0) {
  5. showMap(var.second.variables);
  6. }
  7. }
  8. }

有没有什么想法或建议来解决这个问题?

英文:

I have nested maps and I need to iterate through each level to print all the values.

This map is to show you an example of a problem that I am trying to solve using recursion.

  1. MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow) {
  2. ui-&gt;setupUi(this);
  3. std::map&lt;std::string, VariableContainer&gt; myMap;
  4. for (int i = 0; i &lt; 3; ++i) {
  5. VariableContainer var;
  6. var.variables[&quot;A&quot; + std::to_string(i)];
  7. if (i == 1) {
  8. VariableContainer childVar;
  9. for (int j = 1; j &lt; 3; ++j) {
  10. childVar.variables[&quot;X_&quot; + std::to_string(j)];
  11. }
  12. var.variables.emplace(&quot;B&quot; + std::to_string(i), childVar);
  13. }
  14. var.variables[&quot;C&quot; + std::to_string(i)];
  15. myMap.emplace(&quot;Node_&quot; + std::to_string(i), var);
  16. }
  17. for (auto&amp; var : myMap) {
  18. std::cout &lt;&lt; &quot;var: &quot;&lt;&lt; var.first &lt;&lt; std::endl;
  19. if (var.second.variables.size()&gt;0) {
  20. for (auto&amp; child : var.second.variables) {
  21. std::cout &lt;&lt; &quot; child: &quot;&lt;&lt; child.first &lt;&lt; std::endl;
  22. if (child.second.variables.size()&gt;0) {
  23. for (auto&amp; subChild : child.second.variables) {
  24. std::cout &lt;&lt; &quot; subChild: &quot;&lt;&lt; subChild.first &lt;&lt; std::endl;
  25. }
  26. }
  27. }
  28. }
  29. }
  30. }

This is the output:

  1. var: Node_0
  2. child: A0
  3. child: C0
  4. var: Node_1
  5. child: A1
  6. child: B1
  7. subChild: X_1
  8. subChild: X_2
  9. child: C1
  10. var: Node_2
  11. child: A2
  12. child: C2

But I need to get the output in the following structure: node + &quot;.&quot; + child + &quot;.&quot; + subChild + &quot;.&quot; + nsubChilds

  1. Node_0.A0
  2. Node_0.C0
  3. Node_1.A1
  4. Node_1.B1.X_1
  5. Node_1.B1.X_2
  6. Node_1.C1
  7. Node_2.A2
  8. Node_2.C2

This is the VariableContainer.h

  1. #ifndef VARIABLECONTAINER_H
  2. #define VARIABLECONTAINER_H
  3. #include &lt;map&gt;
  4. #include &lt;string&gt;
  5. class VariableContainer
  6. {
  7. public:
  8. VariableContainer() {}
  9. std::map&lt;std::string, VariableContainer&gt; variables;
  10. };
  11. #endif // VARIABLECONTAINER_H

I could have maps nested within other maps, something like a branch. I think that using recursion is a good solution since the number of nested maps is different on each execution of the program.

For example, I could have something like this: Node_1.B1.X_1.Y_1.Z_1

I tried the following recursive function but I'm not getting the desired output.

  1. void MainWindow::showMap(std::map&lt;std::string, VariableContainer&gt; &amp;map) {
  2. for (auto&amp; var : map) {
  3. std::cout &lt;&lt; &quot;var: &quot;&lt;&lt; var.first &lt;&lt; std::endl;
  4. if (var.second.variables.size() &gt; 0) {
  5. showMap(var.second.variables);
  6. }
  7. }
  8. }

Any Idea or suggestion to solve this?

答案1

得分: 2

将分支名称传递给函数 showMap()

例如:

  1. class VariableContainer
  2. {
  3. public:
  4. VariableContainer() {}
  5. std::map<std::string, VariableContainer> variables;
  6. };
  7. void showMap(
  8. const std::string &BranchName, //我认为这是必要的
  9. const std::map<std::string, VariableContainer> &map
  10. )
  11. {
  12. for (const auto &var : map)
  13. {
  14. std::string VarName = BranchName + var.first;
  15. if (var.second.variables.empty()) //如果是叶子节点
  16. {
  17. std::cout << VarName << std::endl;
  18. }
  19. else
  20. {
  21. showMap(VarName + ".", var.second.variables);
  22. }
  23. }
  24. }
  25. inline void showMap(const std::map<std::string, VariableContainer> &map)
  26. {
  27. showMap("", map);
  28. }
  29. int main(int argc, char *argv[])
  30. {
  31. std::map<std::string, VariableContainer> Root;
  32. {//构建测试数据
  33. {
  34. auto &Node_0 = Root["Node_0"].variables;
  35. Node_0["A0"];
  36. Node_0["C0"];
  37. }
  38. {
  39. auto &Node_1 = Root["Node_1"].variables;
  40. Node_1["A1"];
  41. auto &B1 = Node_1["B1"].variables;
  42. B1["X_1"];
  43. B1["X_2"];
  44. }
  45. {
  46. auto &Node_2 = Root["Node_2"].variables;
  47. Node_2["A2"];
  48. Node_2["C2"];
  49. }
  50. }
  51. showMap(Root);
  52. return 0;
  53. }

这是您提供的代码的翻译部分。

英文:

Pass the branch name to the function showMap().

e.g.

  1. class VariableContainer
  2. {
  3. public:
  4. VariableContainer() {}
  5. std::map&lt;std::string, VariableContainer&gt; variables;
  6. };
  7. void showMap(
  8. const std::string &amp;BranchName, //I think this is necessary
  9. const std::map&lt;std::string, VariableContainer&gt; &amp;map
  10. )
  11. {
  12. for( const auto &amp;var : map )
  13. {
  14. std::string VarName = BranchName + var.first;
  15. if( var.second.variables.empty() ) //if leaf
  16. { std::cout &lt;&lt; VarName &lt;&lt; std::endl; }
  17. else
  18. { showMap( VarName+&quot;.&quot;, var.second.variables ); }
  19. }
  20. }
  21. inline void showMap( const std::map&lt;std::string, VariableContainer&gt; &amp;map )
  22. { showMap( &quot;&quot;, map ); }
  23. int main(int argc, char *argv[])
  24. {
  25. std::map&lt;std::string, VariableContainer&gt; Root;
  26. {//construct the test data
  27. {
  28. auto &amp;Node_0 = Root[&quot;Node_0&quot;].variables;
  29. Node_0[&quot;A0&quot;];
  30. Node_0[&quot;C0&quot;];
  31. }
  32. {
  33. auto &amp;Node_1 = Root[&quot;Node_1&quot;].variables;
  34. Node_1[&quot;A1&quot;];
  35. auto &amp;B1 = Node_1[&quot;B1&quot;].variables;
  36. B1[&quot;X_1&quot;];
  37. B1[&quot;X_2&quot;];
  38. }
  39. {
  40. auto &amp;Node_2 = Root[&quot;Node_2&quot;].variables;
  41. Node_2[&quot;A2&quot;];
  42. Node_2[&quot;C2&quot;];
  43. }
  44. }
  45. showMap( Root );
  46. return 0;
  47. }

huangapple
  • 本文由 发表于 2023年3月1日 08:50:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75598673.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定