最近考慮做個路由表,眾所周知這東西都是用Radix樹或者AVL樹(紅黑樹)造的。俺們是做工程的人,不搞學術,不打算研究這東西。
網上找了半天,終於有位兄弟告訴我們怎麼在VC的STL裡使用其自帶的紅黑樹(原文 http://woshao.com/article/7b4c844029c311dfa592000c295b2b8d/),good,來段例子。
#include "stdafx.h"
#include "windows.h"
#include <set>
#include <xtree>
#include <stdio.h>
using namespace std;
typedef _Tree<_Tset_traits<int, less<int>, allocator<int>, false>> RBTree;
int _tmain()
{
RBTree * poTree = new RBTree(less<int>(), allocator<int>());
for(int i = 35; i < 100; i++)
{
poTree->insert(i);
}
_tprintf(_T("output: %d\n"), *(poTree->begin()));
return 0;
}
很爽吧。
還在研究當中。