简体   繁体   English

在stl地图中进行后序遍历

[英]Postorder traversal in stl map

I am using a stl map on gcc comper which uses a tree to store key,value pairs. 我在gcc comper上使用stl映射,它使用树来存储键值对。 The iterator advances in an inorder fashion so inorder traversal is really easy. 迭代器按顺序进行,因此顺序遍历非常简单。 However one of my output requirements is a postorder traversal. 但是我的一个输出要求是后序遍历。 I have been specifically aksed to use map. 我一直特意使用地图。 Is there any way to get it done? 有没有办法完成它?

There is no standard way to access the "actual tree structure" of an instance of std::map . 没有标准方法来访问std::map实例的“实际树结构”。

Furthermore, the standard doesn't know (or care) exactly how the elements of a map are arranged in any internal tree that the map might use. 此外,该标准不知道(或关心) map元素在地图可能使用的任何内部树中的排列方式。 Red-Black trees and AVL trees are both valid implementations of std::map , and you'd get a different postorder traversal according to which is in fact used. 红黑树和AVL树都是std::map有效实现,你会得到一个不同的后序遍历,实际上是根据它实现的。 In practice I expect its always RB or very similar, but the implementation freedom informs the interface defined by the standard. 在实践中,我希望它总是RB或非常相似,但实现自由通知标准定义的接口。

In short, std::map is not a tree, it's an abstract data structure that can be (and is) implemented using a tree. 简而言之, std::map不是树,它是一个可以(并且是)使用树实现的抽象数据结构。

It might be possible to hack a particular implementation, but probably best not to. 有可能破解特定的实现,但最好不要。 If you want the tree structure of your data to be part of the defined state of your program, you could perhaps write your own tree. 如果您希望数据的树结构成为程序的已定义状态的一部分,则可以编写自己的树。

As Steve Jessop said Map is the abstract data structure provided by c++, you can not change the order of elements stored in map in any order, like we can do in Tree or AVL by traversing in pre/post/in order. 正如Steve Jessop所说Map是c ++提供的抽象数据结构,你不能以任何顺序改变地图中存储的元素的顺序,就像我们可以通过前/后/顺序遍历Tree或AVL一样。 but you can reverse the order of storing by using std::greater as your key instead of std::less . 但是你可以通过使用std::greater作为键而不是std::less来反转存储顺序。

eg 例如

std::map< std::string, int, std::greater<std::string> > my_map; 

OR crate one Vector desiredOrder, which will keep the trac of order of insertion in map, and once you are done with insertion just do whatever sorting operation you want pre/post/ in order on the desiredOrder Vector and using for loop you can traverse the vector and access the Map elements using the vector value as a key for your Map 或者创建一个Vector desiredOrder,它将在地图中保留插入顺序的trac,一旦完成插入,只需在desiredOrder Vector上按顺序执行前/后/顺序排序操作,并使用for循环可以遍历使用矢量值作为Map的关键向量并访问Map元素

eg 例如

std::vector<std::string> desiredOrder;
std::map<std::string, int> myTree;

myTree["A"] = 0;
desiredOrder.push_back("A"); 
myTree["B"] = 0;
desiredOrder.push_back("B");
myTree["C"] = 0;
desiredOrder.push_back("C");
myTree["D"] = 0;
desiredOrder.push_back("D");

/*
Do whatever sorting you want to do here....
*/

for (int i = 0; i < desiredOrder.size(); ++i)
{
    const std::string &s = desiredOrder[i];
    std::cout << s << ' ' << myTree[s] << '\n';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM