26

I can't find simple example how to use queues in C++ for pointers to some myclass objects. I have code like this:

class myclass{
  string s;
};

myclass *p = new myclass();

my_queue.push(p);

//something....

p = my_queue.front();
my_queue.pop();

std::cout << p->s;

What should be declaration of my_queue? Should I use queue or another data structure?

I need c++ just for small program, thanks for answers.

Ondra
  • 3,100
  • 5
  • 37
  • 44

3 Answers3

44

Simply declare it as below if you want to us the STL queue container.

std::queue<myclass*> my_queue;
Nim
  • 33,299
  • 2
  • 62
  • 101
11

std::queue<myclass*> my_queue; will do the job.

See here for more information on this container.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
10

std::queue<myclass*> that's it

RedX
  • 14,749
  • 1
  • 53
  • 76