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-
226k56 gold badges496 silver badges504 bronze badges
asked Sep 28, 2013 at 22:39
12
  • std::deque? Commented Sep 28, 2013 at 22:41
  • 1
    @Oli, wouldn't be much of an assignment if it was allowed Commented Sep 28, 2013 at 22:44
  • pop_front generally returns the value of the first element by the way Commented Sep 28, 2013 at 22:44
  • 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 Commented Sep 28, 2013 at 22:46
  • 2
    @Dgrin91 There's a good reason pop* methods don't return a value. Commented Sep 28, 2013 at 22:57

1 Answer 1

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!

answered Sep 28, 2013 at 22:48
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.