1

Is it possible to construct a queue which is built using its copy constructor that takes a deque as an argument but is also able to manipulate the copied deque? The default behavior is copied data is preserved in its original location which is fine, but it also wouldn't hurt to use a pointer to the deque to create a bound deque-queue combo.

deque<int> Deck {10, 20, 30, 40};
queue<int> DeckQue(Deck);

DeckQue.push(50);

for (auto it = Deck.begin(); it != Deck.end(); ++it) {
     cout << " " << *it;
}

//This still prints 10, 20, 30, 40 I want it to print 10, 20, 30, 40, 50
moonwalker
  • 1,157
  • 1
  • 18
  • 34
  • Why not subclass it and write a ctor that takes one by reference? It could then do both things you write. – Ami Tavory May 12 '15 at 18:55
  • You have `//This still prints 10, 20, 30, 40` which I take it to mean this isn't what you expected to get. What did you expect or what is the desired outcome? – NathanOliver May 12 '15 at 18:55
  • 2
    Why use `queue` at all? – hynner May 12 '15 at 18:56
  • @AmiTavory You [should not subclass](https://stackoverflow.com/questions/2034916/is-it-okay-to-inherit-implementation-from-stl-containers-rather-than-delegate) most of the classes in the standard library. – Cory Kramer May 12 '15 at 18:57
  • @hynner Assuming I have an algorithm that uses queue style push and pop operations. – moonwalker May 12 '15 at 18:58
  • 2
    @BarışÜrüm then you don't want a copy at all. You want a new *interface* to the *same* data. – Drew Dormann May 12 '15 at 18:59
  • @DrewDormann yes and how can I do it? – moonwalker May 12 '15 at 19:00
  • 1
    @BarışÜrüm: If you can change the algorithm to work with deque then you can just use `push_back` and `pop_front` methods of deque, if not then you'll need to create a wrapper over deque that will support both queue and deque api. – hynner May 12 '15 at 19:02
  • @Cyber Point taken. Composition, then. – Ami Tavory May 12 '15 at 19:15
  • 2
    @Cyber Private inheritance is fine. The container adapters are actually meant to be inherited from - they expose the adapted container as a `protected` data member. – T.C. May 12 '15 at 19:21
  • @hynner C++ specification says: "As opposed to std::vector, the elements of a deque are not stored contiguously: typical implementations use a sequence of individually allocated fixed-size arrays." does this hinder the effort to create a FIFO solely with deque using `push_back` and `pop_front`? – moonwalker May 12 '15 at 19:26
  • @BarışÜrüm: No. If you look at the definition of `std::queue` you'll see that it's a template of 2 arguments a type of items stored in the queue and a container with `std::deque` being the default container. – hynner May 12 '15 at 19:44
  • @hynner I'm referring to your comment about using deque without a queue to implement the FIFO container. If deque doesn't support reliable positioning of elements we can't rely on deque to have first in data at front location. – moonwalker May 12 '15 at 19:48
  • 1
    @BarışÜrüm: When you call `front` on `deque` you'll get whatever is the first element, it doesn't matter where is it physically located. – hynner May 12 '15 at 20:04

0 Answers0