Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

using for_each to to push items onto a queue

Posted on 2004-09-01
9
Medium Priority
?
766 Views
Last Modified: 2011-10-03
i want to turn this

for( u = 0; u < graph.size(); ++u )
            Q.push( &graph[u] );

into 1 line using for_each

for_each( graph.begin(), graph.end(), bind1st( mem_fun( &priority_queue<VERTEX*, vector<VERTEX*>, VERTEX >::push ), &Q ) );

soemthing like that is the idea but i cant get it to work. how do i fix it?
0
Comment
Question by:Kitty__Kong
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 3

Expert Comment

by:Indrawati
ID: 11950340
OK, not sure if this is your problem here, but I always have problem using mem_fun on a member function that takes argument(s) on VC++6 (compiler error) . It seems ok though on member function that does not expect any argument.
0
 
LVL 4

Expert Comment

by:anthony_w
ID: 11951459
Download the boost libraries from www.boost.org

Now you can write:

    std::for_each(graph.begin(),graph.end(),
                  boost::lambda::bind(&priority_queue<VERTEX*, vector<VERTEX*>, VERTEX >::push,&Q,&boost::lambda::_1));

after adding

#include <boost/lambda/bind.hpp>
#include <boost/lambda/lambda.hpp>

to your list of includes
0
 
LVL 1

Author Comment

by:Kitty__Kong
ID: 11954457
im using vsnet2k3

i want to to do it without boost
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:Kitty__Kong
ID: 11954478
i forgot to say graph is a vector<VERTEX>

#include <functional>
#include <algorithm>
#include <queue>
#include <vector>
 
using namespace std;
 
int main()
{
   priority_queue<int> q;
   vector<int> g;
 
   for_each(g.begin(), g.end(),
       bind1st(mem_fun(&priority_queue<int>::push), &q));
 
    return 0;
}

there is a simple example of wht i want to work
0
 
LVL 15

Expert Comment

by:efn
ID: 11959955
It's certainly possible to construct something that you can pass to for_each and accomplish the desired goal, but I don't think you can do it in one line.  It would have to be a function object that knows about the queue.  You can write a class for such objects, but I don't think you can whip it up out of standard library functions as you are trying to do.

But you never know, some other expert may yet enlighten us all.

--efn
0
 
LVL 4

Accepted Solution

by:
anthony_w earned 1000 total points
ID: 11961684
Can I ask why you wan't to accomplish this without boost? It's free, and works well with VS.NET2003

As efn says, you can't do this with the standard library, because mem_fun doesn't work well with functions taking a reference. You could use a library that handled things properly (like boost), or you can write your own.

If you want a function object to use with for_each, use something like:

#include <functional>
#include <algorithm>
#include <queue>
#include <vector>
 
using namespace std;

template<typename T>
struct PushOnPriorityQueue
{
    priority_queue<T>& queue;

    PushOnPriorityQueue(priority_queue<T>& queue_):
        queue(queue_)
    {}
   
    void operator()(T const& valueToPush)
    {
        queue.push(valueToPush);
    }
};

template<typename T>
PushOnPriorityQueue<T> pushOnPriorityQueue(priority_queue<T>& queue)
{
    return PushOnPriorityQueue<T>(queue);
}
 
int main()
{
   priority_queue<int> q;
   vector<int> g;
 
   for_each(g.begin(), g.end(),pushOnPriorityQueue(q));
 
   return 0;
}


An alternative is to provide an output iterator that does a push, and use std::copy:

#include <functional>
#include <algorithm>
#include <queue>
#include <vector>
 
using namespace std;

template<typename T>
struct PriorityQueuePusher
{
    priority_queue<T>& queue;

    PriorityQueuePusher(priority_queue<T>& queue_):
        queue(queue_)
    {}
   
    PriorityQueuePusher& operator*()
    {
        return *this;
    }
   
    void operator=(T const& valueToPush)
    {
        queue.push(valueToPush);
    }
    void operator++()
    {}
};

template<typename T>
PriorityQueuePusher<T> makePriorityQueuePusher(priority_queue<T>& queue)
{
    return PriorityQueuePusher<T>(queue);
}
 
int main()
{
   priority_queue<int> q;
   vector<int> g;
 
   copy(g.begin(), g.end(),makePriorityQueuePusher(q));
 
    return 0;
}

0
 
LVL 1

Author Comment

by:Kitty__Kong
ID: 11965875
so what i want is impossible using just the stl? it cant be made to work using functional?
0
 
LVL 15

Expert Comment

by:efn
ID: 11966491
> so what i want is impossible using just the stl? it cant be made to work using functional?

That's my opinion.  I just hesitate to be too dogmatic, because the library is so big and obscure.

By the way, to call a member function of an object passed by reference, you can use mem_fun_ref.  But that doesn't help with this problem.

In addition to the solutions anthony_w showed, it would be possible to craft one using bind1st or bind2nd.  But like those, it would require several lines.

--efn
0
 
LVL 1

Author Comment

by:Kitty__Kong
ID: 11966797
im going to leave the question up a bit longer to see if someone comes up with a way to do what i want. if no one does i will accept one of the already posted responses.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question