In my program, I have a template class that can take as arguments any type of container, like std::vector, std::map, etc.
The goal is that my class can work with both vector iterators and map iterators. Right now it works with vector iterators, for example they do stuff with the vector iterators as arguments. Small example:
template <class T>
class Example
{
private:
public:
Example() {}
void someMethod(typename T::iterator it)
{
cout << *it;
}
};
If I use Example< vector<int> > example
, and use someMethod
with some iterator, the method will just print an int
from the vector.
But if I will use Example < map<int, int> >
and pass a map<int, int>::iterator
as argument in someMethod
it will not work, because I would have to change the *it
in someMethod
to it->first
or it->second
(depending on what I want to print, key or value).
My question: is there a way I could pass a map iterator that would actually be an iterator to the key / value so I don't have to touch any method in my class?