STL/ Iterator question

Status
Not open for further replies.
i've never before used STL. I'm fiddling with a part of a program that is supposed to pop one item off a list, and push it onto the back of another list:

background stuff:
Code:
list<Pcb>::iterator it;
list<Pcb> readyqueue;
list<Pcb> jobqueue;
there is just one item in jobqueue.

(please excuse the massive amounts of debugging/trace stuff :))

Code:
	while(it != jobqueue.end()){
		cout<<"Examining process "<<it->jobNum<<endl;
		cout<<"Entering if: it->state=="<<it->state<<endl;
		cout.flush();
		if(it->state==STATE_READY){
			
			readyqueue.push_back(*it);	// queue up
			cout<<"\tProcess "<<it->jobNum<<" pushed onto readyqueue.\n";
			cout<<"\treadyqueue tail has jobNum = "<<readyqueue.back().jobNum<<endl;
			cout.flush();

			cout<<"\tProcess "<<it->jobNum<<" being removed from jobqueue.\n";
			cout.flush();
			jobqueue.erase(it);			// remove from jobqueue

		}
		cout<<"Outside of if\n";
		cout.flush();
//		getch();
		it++;
	}
	cout<<"All ready processes have been moved from job queue to ready queue.\n";
	cout.flush();

and this is my output for that code segment:
Code:
Examining process 1
Entering if: it->state==3
        Process 1 pushed onto readyqueue.
        readyqueue tail has jobNum = 1
        Process 1 being removed from jobqueue.
Outside of if
and then i get an access violation. the debugger shows the access violation being in the beginning of the loop again, even though the iterator goes beyond the end of the list. so then i'm trying to access variables inside a nonexistant object past the end of the container.

or at least, so it seems like to me.

what am i doing wrong with my iterators, that is making the loop go through too many times?
 
When you call "jobqueue.erase(it);" you invalidate your iterator. Thus you can no longer increment this iterator with defined results. At this point the "it != jobqueue.end()" is meaningless. The situation is slightly excerbated (sp?) because your queue has only one element, if you had more than one your code may have actually succeeded.

One solution is to modify your code to call jobqueue.empty() instead of the testing against jobqueue.end(). Better yet, why aren't you using the queue class provided by STL?
 
because i'll later need to use the features of a list for ordering and what not. basically, this is the shell without the actual "put it in this order and extract in this order" stuff that i'll plug in once i've got the framework running.

but thank you! i changed it to
it = jobqueue.erase(it);

and that broke open the floodgates of things now working as i expect them to! :)
 
but with a priority queue, can i select an element that *isn't* the head, if the head doesn't satisfy other conditions to be selected and removed (available resources, for example)?

f so, then i might redo how i made this project, or at least change part of it so that the next part will work better. (i just finished a basic FCFS cpu scheduler. the next part is SJF- which is why your priority queue suggestion appeals to me)

i chose list because I'm utterly unfamiliar with STL, and didn't want to be wrestling around with limitations of various data structures. performance isn't important; that i get this done by tuesday is :)

now that I actually have a better grasp on this stuff, I'll fool around with it some more when I'm not under time restraints.
 
The priority_queue adaptor provided by STL is pretty basic, so it doesn't allow you to traverse the underlying container, however it very easy to roll your own that does:
Code:
#include <algorithm>
#include <vector>
#include <functional>

template<typename T,
         typename Container = std::vector<T>,
         typename Compare = std::less<typename Container::value_type> >
class mutable_queue
{
public:
    typedef typename Container::value_type;
    typedef typename Container::size_type size_type;
    typedef typename Container::iterator iterator;
    typedef typename Container::const_iterator const_iterator;
    typedef Container container_type;

protected:
    Compare comp;
    Container c;

public:
    explicit
    mutable_queue( const Compare& cmp = Compare(),
                   const Container& cont = Container() )
        : comp(cmp), c(cont)
    {
        std::make_heap( c.begin(), c.end(), comp );
    }

    template<typename InputIterator>
    mutable_queue( InputIterator first, InputIterator last,
		const Compare& cmp = Compare(),
		const Container& cont = Container() )
		: comp(cmp), c(cont)
	{
		 c.insert( c.end(), first, last );
		 std::make_heap( c.begin(), c.end(), comp );
	}

	iterator begin( void )
	{ return c.begin(); }

	iterator end( void )
	{ return c.end(); }

	const_iterator begin( void ) const
	{ return c.begin(); }

	const_iterator end( void ) const
	{ return c.end(); }

	void push( const value_type& v )
	{
		c.push_back( v );
		std::push_heap( c.begin(), c.end(), comp );
	}

	void pop( void )
	{
		std::pop_heap( c.begin(), c.end(), comp );
		c.pop_back();
	}

	bool empty( void ) const
	{ return c.empty(); }

	size_type size( void ) const
	{ return c.size(); }

	const value_type& top( void ) const
	{ return c.front(); }

	void reheap( void )
	{ std::make_heap( c.begin(), c.end(), comp ); }
};
 
Status
Not open for further replies.