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
| #include <iostream>
#include <map>
template <typename T>
class MyClass
{
public:
typedef typename T::key_type key;
typedef typename T::value_type value;
typedef typename T::const_iterator const_iterator;
typedef typename T::iterator iterator;
template <typename iter >
MyClass(iter b, iter e):data_(b,e){}
key getKey(/*...*/) const
{
std::cout<<"try to find the key"<<std::endl;
/* je ne prend que le premier élément, pour la démo ;) */
const_iterator it = data_.begin();
return it->first;
}
value const & findValue(key k) const
{
return data_.find(k)->second;
}
const_iterator begin() const{return data_.begin();}
const_iterator end() const{return data_.end();}
const_iterator find(key k){return data_.find(k);}
private:
T data_;
};
int main()
{
/* par facilité uniquement */
typedef std::map<int, double> map;
std::map<int,double> tab;
tab.insert(std::make_pair(1, 1.22));
tab.insert(std::make_pair(4, 3.22));
tab.insert(std::make_pair(3, 3.1415));
tab.insert(std::make_pair(110, 10.22));
MyClass<map> mc(tab.begin(),tab.end());
std::cout<<"getKey renvoie : "<<mc.getKey()<<std::endl;
for(MyClass<map>::const_iterator it = mc.begin();it!=mc.end();++it)
std::cout<<"iterateur obtenu "<<it->first<<" = "<<it->second<<std::endl;
return 0;
} |