I cannot seem to be able to delete a user inputted value from a queue (it can be any value in the queue). The way I am trying to accomplish this is by using a vector to temporarily store the values from the queue while skipping over the value that is to be "deleted." This way I don't assign a vector element with this value. When I run the switch case 'r' which is to accomplish this task, it will start doing exactly what it is supposed to do by assigning the queue values into the vector. Once front() function for the queue equals the value to be deleted/skipped, an error message will come up on my screen saying that
debug asseration failed. It also mentions that my vector subscript is out of range. I do not understand how this is possible and where am I going wrong in my code?
You never pop from the queue in the loop. You just continuously get the front() element, which is always the same.
If you need to remove an element from the middle then you should not be using a queue. The whole point of a queue is that it may be implemented more efficiently by assuming that the caller will only remove from the front and add at the back.
Just use a list. Hell, even a vector would be better than what you're doing now.
while (!lineNumber.empty()) //empties the whole queue
{
lineNumber.pop();
}
And the front element is not always the same. I am using the ++ operator to increment it since my queue elements are sequential. Look at line 29 below.
case'r':
cout << "User, enter a value between " << lineNumber.front() << " and " << lineNumber.back();
cout << " to delete" << endl;
cout << "Enter value: ";
cin >> val;
while (val < lineNumber.front() || val > lineNumber.back())
{
cout << "Invalid value was entered!" << endl;
cout << "Select a value in the range of " << lineNumber.front() << " and " << lineNumber.back() << endl;
cin.ignore();
cin.clear();
cout << "Enter value: ";
cin >> val;
}
size = lineNumber.size();
cout << "lineNumber.size() = " << lineNumber.size() << endl;
for (int index = 0; index < size; index++)
{
cout << "lineNumber.front() = " << lineNumber.front() << endl;
if (lineNumber.front() != val)
{
temp.push_back(lineNumber.front());
cout << "temp[" << index << "] = " << temp[index] << endl;
}
lineNumber.front()++;
}
I'm using the while loop I presented before to delete all the values in the original queue until it is completely empty. Then I am using this code below, to try and assign the values back into the queue using the vector which shouldn't have the "deleted" or "skipped" value within it anymore. Therefore the queue would be have all the numbers except the user selected "deleted" value. Obviously something is wrong because it crashes my program saying that my vector is out of range or something along those lines. Not sure how..
1 2 3 4
for (int index = 0; index < temp.size(); index++) //inserts the vector values into the queue
{
lineNumber.push(temp[index]);
}
As helios was saying, why are you using a std::queue? It's inefficient to copy everything to another container, just for the sake of deleting 1 item. What if you had millions of items, and deletion was a frequent thing? Even though you don't have that much, imagine if this queue was used in an application that had lots of queues.
helios also said a std::vector would be better for this.
There is also std::deque, one can make it work as a FIFO or LIFO queue / stack. It has push /pop to front and back. IIRC it stores it's data in chunks, so it doesn't have to rewrite / move all the other items if something is inserted or removed.
Linear: the number of calls to the destructor of T is the same as the number of elements erased, the number of calls to the assignment operator of T is no more than the lesser of the number of elements before the erased elements and the number of elements after the erased elements
For this particular thing, it seems less efficient than a vector. But like everything, the choice of container is a balance of options.