c – 使用变体无限嵌套的地图,如何实现其长尾词?

2026-04-16 18:544阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计387个文字,预计阅读时间需要2分钟。

c – 使用变体无限嵌套的地图,如何实现其长尾词?

所以,我正在尝试制作一个无限可嵌套的地图。我可以在其中使用字符串、整数、布尔值等。这是我试过的:

cppstruct NMap { std::map;};

所以,我正在尝试制作无限可嵌套的地图,我可以在其中使用字符串,整数,布尔等.

这是我试过的:

c – 使用变体无限嵌套的地图,如何实现其长尾词?

struct NMap; struct NMap : std::map<std::string, std::variant<NMAP*, std::string, std::any>> {}; // ... NMap* something; something["lorem"]["ipsum"] = "Test"; ^ - No such operator []

哪个是逻辑的,std :: variant没有[]运算符.无论如何在Nestable地图中使用std :: variant?

简单而有点奇怪的东西:

#include <map> #include <string> #include <optional> struct rmap : std::map<std::string, rmap> { std::optional<std::string> value; // could be anything (std::variant, std::any, ...) };

通过一些糖和一些其他有品味的调整,你可以像你想要的那样使用它:

#include <map> #include <string> #include <optional> #include <iostream> struct rmap : std::map<std::string, rmap> { using value_type = std::optional<std::string>; value_type value; operator const value_type&() const { return value; } rmap& operator=(value_type&& v) { value = v; return *this; } friend std::ostream& operator<<(std::ostream& os, rmap& m) { return os << (m.value ? *m.value : "(nil)"); } }; int main() { rmap m; m["hello"]["world"] = std::nullopt; m["vive"]["le"]["cassoulet"] = "Obama"; std::cout << m["does not exist"] << '\n'; // nil std::cout << m["hello"]["world"] << '\n'; // nil std::cout << m["vive"]["le"]["cassoulet"] << '\n'; // Obama }

你可以用一些语法糖调整你的口味.

本文共计387个文字,预计阅读时间需要2分钟。

c – 使用变体无限嵌套的地图,如何实现其长尾词?

所以,我正在尝试制作一个无限可嵌套的地图。我可以在其中使用字符串、整数、布尔值等。这是我试过的:

cppstruct NMap { std::map;};

所以,我正在尝试制作无限可嵌套的地图,我可以在其中使用字符串,整数,布尔等.

这是我试过的:

c – 使用变体无限嵌套的地图,如何实现其长尾词?

struct NMap; struct NMap : std::map<std::string, std::variant<NMAP*, std::string, std::any>> {}; // ... NMap* something; something["lorem"]["ipsum"] = "Test"; ^ - No such operator []

哪个是逻辑的,std :: variant没有[]运算符.无论如何在Nestable地图中使用std :: variant?

简单而有点奇怪的东西:

#include <map> #include <string> #include <optional> struct rmap : std::map<std::string, rmap> { std::optional<std::string> value; // could be anything (std::variant, std::any, ...) };

通过一些糖和一些其他有品味的调整,你可以像你想要的那样使用它:

#include <map> #include <string> #include <optional> #include <iostream> struct rmap : std::map<std::string, rmap> { using value_type = std::optional<std::string>; value_type value; operator const value_type&() const { return value; } rmap& operator=(value_type&& v) { value = v; return *this; } friend std::ostream& operator<<(std::ostream& os, rmap& m) { return os << (m.value ? *m.value : "(nil)"); } }; int main() { rmap m; m["hello"]["world"] = std::nullopt; m["vive"]["le"]["cassoulet"] = "Obama"; std::cout << m["does not exist"] << '\n'; // nil std::cout << m["hello"]["world"] << '\n'; // nil std::cout << m["vive"]["le"]["cassoulet"] << '\n'; // Obama }

你可以用一些语法糖调整你的口味.