#include <map>
#include <iostream>
int main( )
{
using namespace std;
map <int, int> m1;
map <int, int> :: iterator m1_Iter;
map <int, int> :: const_iterator m1_cIter;
typedef pair <int, int> Int_Pair;
m1.insert ( Int_Pair ( 1, 1 ) );
m1.insert ( Int_Pair ( 2, 2 ) );
m1.insert ( Int_Pair ( 3, 3 ) );
m1.insert ( Int_Pair ( 5, 5) );
m1.insert ( Int_Pair ( 11, 11 ) );
m1.insert ( Int_Pair ( 4, 4 ) );
m1_cIter = m1.begin ( );
for (;m1.end() != m1_cIter; m1_cIter++)
{
cout << "The first element of m1 is " << m1_cIter -> first << endl;
}
}
map中遍历迭代器为何是按照键从小到大遍历的
红黑树是每个节点都带有颜色属性的二叉查找树,颜色或红色或黑色。在二叉查找树强制一般要求以外,对于任何有效的红黑树我们增加了如下的额外要求:
性质1. 节点是红色或黑色。
性质2. 根节点是黑色。
性质3 每个叶节点(NIL节点,空节点)是黑色的。
性质4 每个红色节点的两个子节点都是黑色。(从每个叶子到根的所有路径上不能有两个连续的红色节点)
性质5. 从任一节点到其每个叶子的所有路径都包含相同数目的黑色节点。