1

For the assignment I need to make a custom vector class that can be used as the container for a queue where all assignments are O(1).

I'm trying to implement a pop_front function in my vector to do this, but it isn't working. Here's what I have for it.

Void pop_front(){
  arr += sizeof(T);
  siz--;
}

arr is a dynamically allocated array I'm using for the container, and siz is its current size. Is there something fundamentally wrong with what I'm trying to do?

Ry-
  • 218,210
  • 55
  • 464
  • 476
d0m1n1c
  • 157
  • 4
  • 16
  • [`std::deque`](http://en.cppreference.com/w/cpp/container/deque)? – Oliver Charlesworth Sep 28 '13 at 22:41
  • 1
    @Oli, wouldn't be much of an assignment if it was allowed – Leeor Sep 28 '13 at 22:44
  • `pop_front` generally returns the value of the first element by the way – David says Reinstate Monica Sep 28 '13 at 22:44
  • Looks wrong to me, but who can really say without seeing the rest of the code. – john Sep 28 '13 at 22:45
  • 1
    @user2758011, you should probably post more code, but trying to implement a queue manually over a vector/array, would probably include some head/tail indices that you don't seem to have here – Leeor Sep 28 '13 at 22:46
  • @Dgrin91: [No it doesn't ;)](http://en.cppreference.com/w/cpp/container/deque/pop_front) – Oliver Charlesworth Sep 28 '13 at 22:46
  • Could work, just don't try to `delete[] arr`, you need a separate variable with the original address to delete. – Bernhard Barker Sep 28 '13 at 22:48
  • @OliCharlesworth I said generally, not always, because [often](http://docs.oracle.com/javase/7/docs/api/java/util/Queue.html#remove()) [they](http://docs.python.org/2/library/queue.html) [do](http://docs.oracle.com/javase/7/docs/api/java/util/List.html#remove(int)) – David says Reinstate Monica Sep 28 '13 at 22:54
  • @Dgrin91: Sure, but those are different languages. There are well-established idioms in C++, so the OP should aim to adhere to them. – Oliver Charlesworth Sep 28 '13 at 22:55
  • 2
    @Dgrin91 There's a [good reason](http://stackoverflow.com/questions/12600330/pop-back-return-value) `pop*` methods don't return a value. – jrok Sep 28 '13 at 22:57
  • @jrok Now that is an actually good reason. Kudos to you. – David says Reinstate Monica Sep 28 '13 at 23:00
  • sorry, I'd post the rest of the code, but I don't think my instructor would appreciate that very much. What I was trying to do, is move the pointer for my dynamic array of size n, one space forward, effectively popping off the front, but it won't compile. I guess c++ doesn't like this. I think I may need to implement a head and tail as leeor said. Thanks – d0m1n1c Sep 29 '13 at 21:29

1 Answers1

0

I suppose that your array container is a class that will contain the size of your included elements. Simply knock off the first element by ignoring it! YOU control how the user accesses each element (through its index) with the operator[]() function, so after pop_front(), you change the way the array is accessed, in a way that ignores the first element through operator[]().

Simple enough? Hope so! It needs some work and some testing, but you can do it!

The Quantum Physicist
  • 24,987
  • 19
  • 103
  • 189
  • Thanks, I was considering this, but thought it might be too inefficient memory wise. Looking at it again, we were only given the the time complexity requirement, nothing in regard to memory. – d0m1n1c Sep 29 '13 at 21:33