要將一個map<T1,T2>中的數據導入到一個vector<T2>中,可以考慮使用標準庫中的transform,但map<T1,T2>::iterator與vector<T2>::iterator是不匹配的,受《Effective STL》第20條啟發寫個map2vector的functor,可以解決這個問題(不過這個還不算真正的meta-programming):
#include?<map>
#include?<vector>
#include?<iostream>
#include?<string>
#include?<iterator>
using?namespace?std;
struct?map2vector
{
????template<typename?T>
????const?typename?T::second_type&?operator()(const?T&?p)
????????{
????????????return?p.second;
????????}
};
int?main()
{
????map<string,string>?m;
????vector<string>?v;
????m["key1"]="sdf111";
????m["k2"]="sdf11";
????m["k3"]="sdf2";
????transform(m.begin(),m.end(),back_inserter(v),map2vector());
????copy(v.begin(),v.end(),ostream_iterator<string>(cout,"\n"));
??? return?0;
}