Hi! I am wondering how to access the different elements held within a queue. For example, in the following code I use push to fill the queue with 4 vectors, then use front to access the element most distant from the input. Then I use pop to get rid of that, and access the new most distant element, until the queue is empty. I want to be able to load the queue with some vectors (say four of them), print the vector second from input, load the queue with another vector, print the new second vector, and so on. Anyone know how? Thanks!
You aren't putting vectors into that queue. You are pushing in ints. And there is no way to access something other than the front of a queue; the whole point of the queue is that it is a FIFO structure. Random access isn't a part of what it does.
The real solution though is to use deque instead. It makes no difference as far as the FIFO functionality is concerned (although the method names you will use will be different). But deque will also give you random access. Same goes for using vector instead of stack. You can't substitute priority queue so easily with some of the container classes, but random access makes no sense there, so it's ok.
Zhuge, Thanks! I just realized that I was loading the queue with ints. Dumb mistake. And thanks for telling me that "random access" is the word for the trait I am looking for.
Simeonz, Thanks for telling me deque allows random access. I'll try to use it instead.
Is it possible to nest STL containers? For example, can you create a deque that holds vectors or lists?
Is it possible to nest STL containers? For example, can you create a deque that holds vectors or lists?
Yes, but remember that containers are not light, and overwriting one container which is element in another container with some new value is heavy operation.