using Functor for map values?

T

tomerdr

Hi,
I have the following function and functor and i want to print all
values of a map
but pair does not support operator << of course.
so i wonder what are my options?
can i extend the pair struct? can i create a values iterator?
how do i do it?

Thanks in advance.

template <class Iterator, class Function>
void apply(Iterator begin, Iterator end, const Function& f)
{
for(;begin != end; ++begin)
f(*begin);
}

class Print
{
public:
template<class T> void operator()(const T& printable) const
{
cout << printable << endl;
}
};
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Hi,
I have the following function and functor and i want to print all
values of a map
but pair does not support operator << of course.
so i wonder what are my options?
can i extend the pair struct? can i create a values iterator?
how do i do it?

Thanks in advance.

template <class Iterator, class Function>
void apply(Iterator begin, Iterator end, const Function& f)
{
for(;begin != end; ++begin)
f(*begin);

}

class Print
{
public:
template<class T> void operator()(const T& printable) const
{
cout << printable << endl;
}

};

Do you want to print the key-value pair of just the value? If you want
the second you could change the line 'f(*begin);' to 'f(begin-
second);', this would however make the function a lot less general. A
better approach, which works regardless if you want to print just the
value or the key-value pair, is to create an overloaded version of the
()-operator.

class Print
{
public:
template<class T>
void operator()(const T& printable) const
{
cout << printable << endl;
}
template<class T, class U>
void operator()(const pair<T, U>& printable) const
{
cout << printable.first << ": " << printable.second << endl;
}
};
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

I want to print the value only and it must be a general functor.

First of all, try to quote relevant sections of the post you are
replying to.

No, either you can take the code I posted in the previous message and
modify it a bit so that it only prints the value. Or you can define a
<<-operator for std::pair:

template<class T, class U>
std::eek:stream& operator<<(std::eek:stream& os, const std::pair<T, U>& p)
{
os << p.second;
return os;
}
 
D

dasjotre

Or you can define a
<<-operator for std::pair:

template<class T, class U>
std::eek:stream& operator<<(std::eek:stream& os, const std::pair<T, U>& p)
{
os << p.second;
return os;

}

Wouldn't that fail due to ADL unless it is put into std namespace?
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Wouldn't that fail due to ADL unless it is put into std namespace?

Hmm, don't know. It worked in my little test program but that was just
one small file with all the code in it, so there might be cases where
it won't work.
 
D

Dave Rahardja

Wouldn't that fail due to ADL unless it is put into std namespace?

No. ADL will _include_ any operator<<(std::eek:stream&, const std::pair<T,U>&) in
std in the list of candidate functions, but it will not _exclude_ the version
that is defined in your current scope.

-dr
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top