1

I have a class, something like:

class A : std::queue<double>
{
  [...]

  void foo();
};

Inside foo() I want to iterate through its elements, but I can't seem to get the syntax right.

I assumed it would be something like: for(auto elem : *this) {} but that doesn't work (a long list of compiler errors). What is the correct syntax?

Daniel Frey
  • 54,116
  • 13
  • 115
  • 176
Lieuwe
  • 1,681
  • 1
  • 24
  • 39
  • [One should not inherit from standard containers](http://stackoverflow.com/questions/6806173/subclass-inherit-standard-containers). Also, [Queue does not store it's values in order](http://stackoverflow.com/questions/16893530/iterate-through-queue-of-objects-in-order) – Mooing Duck Apr 30 '15 at 17:01
  • 1
    @MooingDuck That second link is for Java. Just sayin'... ;) – Daniel Frey Apr 30 '15 at 17:06
  • Actually neither of @MooingDuck's links are relevant. `std::queue` is not `java.util.Queue`, and `std::queue` is not a container. – Ben Voigt Apr 30 '15 at 17:09

1 Answers1

1

The queue can not be iterated directly, however it has a protected member c which is the underlying container. Hence this should work for your case:

for( auto elem : c ) { ... }

Live example

Daniel Frey
  • 54,116
  • 13
  • 115
  • 176