递归STL映射

发布于 2024-09-27 17:56:50 字数 414 浏览 1 评论 0原文

我正在尝试制作一棵地图树(或者只是将一个地图的值指向另一个地图),但我不太确定如何解决这个问题。我找到了关于此的讨论: http://bytes.com /topic/c/answers/131310-how-build-recursive-map 但我对那里发生的事情有点困惑。

例如,我的键是一个字符,我的值是下一个地图。这是假设的声明:

map< char, map< char, map< char.......>>>>>>>>>> root_map;

I'm trying to make a tree of maps (or just have values of a map point to another map), but I'm not too sure how to approach this. I found a discussion about this: http://bytes.com/topic/c/answers/131310-how-build-recursive-map but I'm a little confused on what's going on there.

For example, my key is a char, and my value is the next map. Here's the hypothetical declaration:

map< char, map< char, map< char.......>>>>>>>>>> root_map;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

·深蓝 2024-10-04 17:56:50

也许您正在想这样的事情:

#include <iostream>
#include <map>

template <typename Key, typename Value>
struct Tree
{
    typedef std::map<Key, Tree> Children;

    Tree& operator=(const Value& value) { value_ = value; return *this; }

    Tree& operator[](const Key& key) { return children_[key]; }

    Children children_;
    Value value_;

    friend std::ostream& operator<<(std::ostream& os, const Tree& tree)
    {
        os << tree.value_ << " { ";
        for (typename Children::const_iterator i = tree.children_.begin();
                i != tree.children_.end(); ++i)
            os << i->first << " -> " << i->second << " | ";
        return os << '}';
    }
};

int main()
{
    Tree<int, std::string> t;
    t[1].children_[1] = "one,one";
    t[1].children_[9] = "one,nine";
    t[1] = "hmmm";
    std::cout << t << '\n';
}

我真的不会推荐它。

Maybe you're thinking of something like:

#include <iostream>
#include <map>

template <typename Key, typename Value>
struct Tree
{
    typedef std::map<Key, Tree> Children;

    Tree& operator=(const Value& value) { value_ = value; return *this; }

    Tree& operator[](const Key& key) { return children_[key]; }

    Children children_;
    Value value_;

    friend std::ostream& operator<<(std::ostream& os, const Tree& tree)
    {
        os << tree.value_ << " { ";
        for (typename Children::const_iterator i = tree.children_.begin();
                i != tree.children_.end(); ++i)
            os << i->first << " -> " << i->second << " | ";
        return os << '}';
    }
};

int main()
{
    Tree<int, std::string> t;
    t[1].children_[1] = "one,one";
    t[1].children_[9] = "one,nine";
    t[1] = "hmmm";
    std::cout << t << '\n';
}

I wouldn't really recommend it.

浮云落日 2024-10-04 17:56:50

我不太确定你想要实现什么,但是当我听到“地图树”时,我想到了以下内容:

class NodeData
{
    // Some stuff...
};

class TreeNode
{
public:
    NodeData* data;
    std::map<char, TreeNode*> children;
};

I'm not excatly sure what you want to achieve, but when I hear "tree of maps" I think of the following:

class NodeData
{
    // Some stuff...
};

class TreeNode
{
public:
    NodeData* data;
    std::map<char, TreeNode*> children;
};
翻了热茶 2024-10-04 17:56:50

作为想法,像这样:

struct CharMap {
    std::map<char,CharMap> map;
} root_map;

并像这样使用它

root_map.map['a'].map['b'];

也许您可以通过 CharMap 上的附加方法和运算符使其更加奇特,这将在访问您的结构时消除 .map 的需要。

As idea, something like this:

struct CharMap {
    std::map<char,CharMap> map;
} root_map;

and use it like

root_map.map['a'].map['b'];

Probably you could make it more fancy with additional methods and operators on the CharMap that would eleminate the need of the .map when accessing your structure.

醉生梦死 2024-10-04 17:56:50

是的,你可以。为了让地图做任何有用的事情,您必须使用方法(在本例中为 Set 和 Get)来装饰它。

#include <map>
#include <iostream>

class Clever : public std::map <int, Clever>
{
  public:
    Clever & Set (int i) { m_i = i; return *this; }
    int Get (void) { return m_i; }

  private:
    int m_i;
};

int main (void)
{
  Clever c;
  c[0][2][3].Set(5);

  std::cout << c[0][2][3].Get() << std::endl;

  return 0;
}

Yes you can. In order for the map to do anything useful though, you're going to have to decorate it with methods (in this case, Set and Get).

#include <map>
#include <iostream>

class Clever : public std::map <int, Clever>
{
  public:
    Clever & Set (int i) { m_i = i; return *this; }
    int Get (void) { return m_i; }

  private:
    int m_i;
};

int main (void)
{
  Clever c;
  c[0][2][3].Set(5);

  std::cout << c[0][2][3].Get() << std::endl;

  return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文