2 Replies - 244 Views - Last Post: 20 November 2012 - 09:41 PM Rate Topic: -----

#1 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Queue STL container's pop member function refuses to work.

Posted 20 November 2012 - 09:13 PM

An identical code of the following was able to work successfully work with a <stack> container; except line 22 would use .top() , instead of .front() member function.

Error message reads: pop_front is not member of std::vector<_Ty>'

#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int main()
{
	
	vector <int> vecOfInts;

	vecOfInts.push_back(4);
	vecOfInts.push_back(8);
	vecOfInts.push_back(3);
	vecOfInts.push_back(5);
	vecOfInts.push_back(0);
	vecOfInts.push_back(8);

	queue < int, vector<int> > qOfInts(vecOfInts);

	for (int i = 0; i < vecOfInts.size(); i++)
	{
		cout << "Front: " << qOfInts.front() << "  ";
		qOfInts.pop();		// pop value from front.
	}

	cout << endl;

	system("pause");
	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Queue STL container's pop member function refuses to work.

#2 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 352
  • View blog
  • Posts: 770
  • Joined: 27-June 09

Re: Queue STL container's pop member function refuses to work.

Posted 20 November 2012 - 09:40 PM

Vector does not have a pop_front method. Only a list or a dequeue can be used as the underlying structure for a queue. Cite. If you look at stack, vectors do support the operations for it, so vectors can be the underlying structure for stacks.

This post has been edited by mojo666: 20 November 2012 - 09:41 PM

Was This Post Helpful? 1
  • +
  • -

#3 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Queue STL container's pop member function refuses to work.

Posted 20 November 2012 - 09:41 PM

queue requires that there be a pop_front and push_back member for the implementing class hence a deque is the default container used.

the following works just fine.

#include <iostream>
#include <queue>
#include <deque>
using namespace std;

int main()
{

	deque <int> vecOfInts;

	vecOfInts.push_back(4);
	vecOfInts.push_back(8);
	vecOfInts.push_back(3);
	vecOfInts.push_back(5);
	vecOfInts.push_back(0);
	vecOfInts.push_back(8);

	queue < int, deque<int> > qOfInts(vecOfInts);

	for (int i = 0; i < vecOfInts.size(); i++)
	{
		cout << "Front: " << qOfInts.front() << "  ";
		qOfInts.pop();		// pop value from front.
	}

	cout << endl;
	return 0;
}



luckily however deque is the default so we can do the following as well

queue <int> qOfInts(vecOfInts);



and the program is equivalent(I bet the compiled binaries are even the same if you did a checksum of them)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1