1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
#include <map>
#include <iostream>
#include <string>
template <typename I, typename T>
class Tree : public std::map<I, Tree<I, T>>
{
public:
Tree() {}
Tree(T val) { mValue = val; }
T operator=(const T& rhs) { return mValue = rhs; }
using std::map<I, Tree<I, T>>::operator=;
T value() { return mValue; }
private:
T mValue;
};
template <typename T>
std::string ToString(T val)
{
return std::to_string(val);
}
template <>
std::string ToString(std::string val)
{
return val;
}
template <typename I, typename T>
void PrintTree(LC::Tree<I, T> tree, std::string prefix)
{
prefix += "/";
std::cout << prefix << tree.value() << std::endl;
for(typename LC::Tree<I, T>::iterator iter = tree.begin(); iter != tree.end(); ++iter)
{
std::string tmp = prefix;
tmp += ToString(iter->first);
PrintTree(iter->second, tmp);
}
}
int main(int argc, char* argv[])
{
typedef std::string key;
typedef std::string value;
LC::Tree<key, value> tree("100");
tree["0"] = "5";
tree["0"]["1"] = "10";
tree["1"] = "15";
tree["1"]["0"] = "20";
tree["1"]["0"]["0"] = "25";
std::map<key, LC::Tree<key, value>> map;
map = tree;
tree["2"] = map;
tree["2"] = tree.value();
PrintTree(tree, "");
LC::Tree<int, int> tree2(100);
tree2[0] = 5;
tree2[0][0] = 32;
PrintTree(tree2, "");
return 0;
}
|