嵌套地图的比较器

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

comparator for nested map

问题

我有一个包含另一个地图作为值的地图。

外部地图包含字符串名称/自定义类(在此示例中,我以名称为例),内部地图包含日期时间和值。

我希望对外部地图运行CompareNames,对内部地图运行CompareDateTime。在结构体A的MyMap初始化列表中传递比较器时,我能否请得到一些帮助,看我在做什么错误。

  1. #include <iostream>
  2. #include <map>
  3. #include <locale>
  4. #include <string>
  5. #include <boost/date_time/posix_time/posix_time.hpp>
  6. #include <boost/date_time/posix_time/posix_time_io.hpp>
  7. enum class ComparePolicy
  8. {
  9. custom1,
  10. custom2
  11. };
  12. struct CompareNames
  13. {
  14. explicit CompareNames(ComparePolicy policy)
  15. : policy(policy)
  16. {}
  17. template <typename T>
  18. bool operator()(const T& lhs, const T& rhs) const
  19. {
  20. if (policy == ComparePolicy::custom1)
  21. {
  22. return lhs < rhs;
  23. }
  24. else
  25. {
  26. return lhs > rhs;
  27. }
  28. }
  29. ComparePolicy policy;
  30. };
  31. struct CompareDateTime
  32. {
  33. explicit CompareDateTime(ComparePolicy policy)
  34. : policy(policy)
  35. {}
  36. template <typename T>
  37. bool operator()(const T& lhs, const T& rhs) const
  38. {
  39. const boost::posix_time::ptime timelhs =
  40. boost::posix_time::time_from_string(lhs);
  41. const boost::posix_time::ptime timerhs =
  42. boost::posix_time::time_from_string(rhs);
  43. if (policy == ComparePolicy::custom1)
  44. {
  45. return timelhs < timerhs;
  46. }
  47. else
  48. {
  49. return timelhs > timerhs;
  50. }
  51. }
  52. ComparePolicy policy;
  53. };
  54. struct A
  55. {
  56. explicit A(ComparePolicy dateTime, ComparePolicy names)
  57. : MyMap(CompareNames(names), CompareDateTime(dateTime))
  58. {}
  59. void fillMe()
  60. {
  61. MyMap["alpha"]["1981-08-20 08:05:00"] = 1;
  62. MyMap["alpha"]["1981-08-20 10:05:00"] = 1;
  63. MyMap["alpha"]["1981-08-20 09:05:00"] = 1;
  64. MyMap["gamma"]["1981-08-20 08:05:00"] = 1;
  65. MyMap["gamma"]["1981-08-20 10:05:00"] = 1;
  66. MyMap["gamma"]["1981-08-20 09:05:00"] = 1;
  67. MyMap["beta"]["1981-08-20 08:05:00"] = 1;
  68. MyMap["beta"]["1981-08-20 10:05:00"] = 1;
  69. MyMap["beta"]["1981-08-20 09:05:00"] = 1;
  70. }
  71. void printMe()
  72. {
  73. for (auto& item : MyMap)
  74. {
  75. for (auto& entry : item.second)
  76. {
  77. std::cout << item.first << " : " << entry.first << " : " << entry.second << std::endl;
  78. }
  79. }
  80. }
  81. std::map<std::string, std::map<std::string, int, CompareDateTime>, CompareNames> MyMap;
  82. };
  83. int main()
  84. {
  85. A test(ComparePolicy::custom1, ComparePolicy::custom2);
  86. test.fillMe();
  87. test.printMe();
  88. return 0;
  89. }

colliru链接: http://coliru.stacked-crooked.com/a/2bdfbf3bd96ed17e

英文:

I have a map which contains another map as value.

Outer map contains string names/custom class (in this example i took name as example), inside map contain datetime and value.

I want CompareNames to be running for outer map and CompareDateTime to be running for inside map. Can i please get some help with what am i doing wrong while passing the comparators to the MyMap initializer list in struct A.

  1. #include &lt;iostream&gt;
  2. #include &lt;map&gt;
  3. #include &lt;locale&gt;
  4. #include &lt;string&gt;
  5. #include &lt;boost/date_time/posix_time/posix_time.hpp&gt;
  6. #include &lt;boost/date_time/posix_time/posix_time_io.hpp&gt;
  7. enum class ComparePoilicy
  8. {
  9. custom1,
  10. custom2
  11. };
  12. struct CompareNames
  13. {
  14. explicit CompareNames(ComparePoilicy policy)
  15. : policy(policy)
  16. {}
  17. template &lt;typename T&gt;
  18. bool operator()(const T&amp; lhs, const T&amp; rhs) const
  19. {
  20. if (policy == ComparePoilicy::custom1)
  21. {
  22. return lhs &lt; rhs;
  23. }
  24. else
  25. {
  26. return lhs &gt; rhs;
  27. }
  28. }
  29. ComparePoilicy policy;
  30. };
  31. struct CompareDateTime
  32. {
  33. explicit CompareDateTime(ComparePoilicy policy)
  34. : policy(policy)
  35. {}
  36. template &lt;typename T&gt;
  37. bool operator()(const T&amp; lhs, const T&amp; rhs) const
  38. {
  39. const boost::posix_time::ptime timelhs =
  40. boost::posix_time::time_from_string(lhs);
  41. const boost::posix_time::ptime timerhs =
  42. boost::posix_time::time_from_string(rhs);
  43. if (policy == ComparePoilicy::custom1)
  44. {
  45. return timelhs &lt; timerhs;
  46. }
  47. else
  48. {
  49. return timelhs &gt; timerhs;
  50. }
  51. }
  52. ComparePoilicy policy;
  53. };
  54. struct A
  55. {
  56. explicit A(ComparePoilicy dateTime, ComparePoilicy names)
  57. : MyMap( CompareNames(names), CompareDateTime(dateTime))
  58. {}
  59. void fillMe()
  60. {
  61. MyMap[&quot;alpha&quot;][&quot;1981-08-20 08:05:00&quot;] = 1;
  62. MyMap[&quot;alpha&quot;][&quot;1981-08-20 10:05:00&quot;] = 1;
  63. MyMap[&quot;alpha&quot;][&quot;1981-08-20 09:05:00&quot;] = 1;
  64. MyMap[&quot;gamma&quot;][&quot;1981-08-20 08:05:00&quot;] = 1;
  65. MyMap[&quot;gamma&quot;][&quot;1981-08-20 10:05:00&quot;] = 1;
  66. MyMap[&quot;gamma&quot;][&quot;1981-08-20 09:05:00&quot;] = 1;
  67. MyMap[&quot;beta&quot;][&quot;1981-08-20 08:05:00&quot;] = 1;
  68. MyMap[&quot;beta&quot;][&quot;1981-08-20 10:05:00&quot;] = 1;
  69. MyMap[&quot;beta&quot;][&quot;1981-08-20 09:05:00&quot;] = 1;
  70. }
  71. void printMe()
  72. {
  73. for (auto&amp; item : MyMap)
  74. {
  75. for (auto&amp; entry : item.second)
  76. {
  77. std::cout &lt;&lt; item.first &lt;&lt; &quot; : &quot; &lt;&lt; entry.first &lt;&lt; &quot; : &quot; &lt;&lt; entry.second &lt;&lt; std::endl;
  78. }
  79. }
  80. }
  81. std::map&lt;std::string, std::map&lt;std::string, int, CompareDateTime&gt;, CompareNames&gt; MyMap;
  82. };
  83. int main()
  84. {
  85. A test(ComparePoilicy::custom1, ComparePoilicy::custom2);
  86. test.fillMe();
  87. test.printMe();
  88. return 0;
  89. }

colliru link: http://coliru.stacked-crooked.com/a/2bdfbf3bd96ed17e

i tried searching for simillar issues, reading https://en.cppreference.com/w/cpp/container/map/map and trying to get the solution and playing with the initializer list in struct A.

答案1

得分: 1

简而言之,要使用std::map::operator[],映射类型必须是可默认构造的,并且您的std::map使用的比较器不能是默认构造的。

您可以通过避免需要映射类型是可默认构造的成员函数来解决此问题:

  1. struct A {
  2. explicit A(ComparePoilicy dateTime, ComparePoilicy names) :
  3. MyMap(CompareNames(names)),
  4. m_dateTime(dateTime) // 存储用于创建内部映射的日期时间比较器
  5. {}
  6. // 用于插入具有正确初始化比较器的值的辅助函数
  7. void add_one(const std::string& key1, const std::string& key2, int val) {
  8. if (not MyMap.contains(key1)) {
  9. MyMap.emplace(key1,
  10. std::map<std::string, int, CompareDateTime>
  11. (CompareDateTime(m_dateTime)));
  12. }
  13. // 使用std::map::at是避免问题的一种方法:
  14. MyMap.at(key1).emplace(key2, val);
  15. }
  16. void fillMe() {
  17. // 使用辅助函数:
  18. add_one("alpha", "1981-08-20 08:05:00", 1);
  19. add_one("beta", "1981-08-20 08:05:00", 1);
  20. add_one("gamma", "1981-08-20 08:05:00", 1);
  21. }
  22. std::map<std::string,
  23. std::map<std::string, int, CompareDateTime>, CompareNames> MyMap;
  24. // 用于存储内部映射的比较器参数:
  25. ComparePoilicy m_dateTime;
  26. };

演示链接

英文:

In short, to use std::map::operator[], the mapped type must be default constructible and your std::map uses a comparator that is not.

You can work around it by a avoiding member functions that require the mapped type to be default constructible:

  1. struct A {
  2. explicit A(ComparePoilicy dateTime, ComparePoilicy names) :
  3. MyMap(CompareNames(names)),
  4. m_dateTime(dateTime) // store for when inner maps are to be created
  5. {}
  6. // a helper function to insert values with a properly initialized comparator
  7. void add_one(const std::string&amp; key1, const std::string&amp; key2, int val) {
  8. if(not MyMap.contains(key1)) {
  9. MyMap.emplace(key1,
  10. std::map&lt;std::string, int, CompareDateTime&gt;
  11. (CompareDateTime(m_dateTime)));
  12. }
  13. // using std::map::at is one way to avoid the problem:
  14. MyMap.at(key1).emplace(key2, val);
  15. }
  16. void fillMe() {
  17. // using the helper function:
  18. add_one(&quot;alpha&quot;, &quot;1981-08-20 08:05:00&quot;, 1);
  19. add_one(&quot;beta&quot;, &quot;1981-08-20 08:05:00&quot;, 1);
  20. add_one(&quot;gamma&quot;, &quot;1981-08-20 08:05:00&quot;, 1);
  21. }
  22. std::map&lt;std::string,
  23. std::map&lt;std::string, int, CompareDateTime&gt;, CompareNames&gt; MyMap;
  24. // to store the comparator argument for the inner maps:
  25. ComparePoilicy m_dateTime;
  26. };

Demo

huangapple
  • 本文由 发表于 2023年2月7日 03:43:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365857.html
匿名

发表评论

匿名网友

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

确定