using Functor for map values?

Discussion in 'C++' started by tomerdr@gmail.com, Feb 1, 2007.

  1. Guest

    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;
    }
    };
    , Feb 1, 2007
    #1
    1. Advertising

  2. Re: using Functor for map values?

    On Feb 1, 10:38 am, wrote:
    > 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;
    }
    };

    --
    Erik Wikström
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Feb 1, 2007
    #2
    1. Advertising

  3. Guest

    Re: using Functor for map values?

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

    Thanks.
    , Feb 1, 2007
    #3
  4. Re: using Functor for map values?

    On Feb 1, 11:38 am, wrote:
    > 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;
    }

    --
    Erik Wikström
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Feb 1, 2007
    #4
  5. dasjotre Guest

    Re: using Functor for map values?

    > 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?
    dasjotre, Feb 2, 2007
    #5
  6. Re: using Functor for map values?

    On Feb 2, 4:11 pm, "dasjotre" <> wrote:
    > > 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?


    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.

    --
    Erik Wikström
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Feb 2, 2007
    #6
  7. Re: using Functor for map values?

    On 2 Feb 2007 07:11:45 -0800, "dasjotre" <> wrote:

    >> 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?


    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
    Dave Rahardja, Feb 2, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Erik Arner
    Replies:
    0
    Views:
    1,310
    Erik Arner
    Nov 2, 2004
  2. Angus
    Replies:
    12
    Views:
    678
    James Kanze
    Oct 2, 2007
  3. kl
    Replies:
    7
    Views:
    1,284
    James Kanze
    Jan 1, 2008
  4. nguillot
    Replies:
    14
    Views:
    2,333
    nguillot
    Jan 29, 2008
  5. Dijkstra
    Replies:
    12
    Views:
    617
    gpderetta
    Jul 10, 2008
Loading...

Share This Page