Hello, I am having trouble deleting a specific element within my Deque.
The Deque holds pointers from the object <Phone Call *>
std::deque<Phone_Call *> Call_Queue;
I am iterating through the Call_Queue & displaying the Caller_ID with:
1 2 3 4
for (int i = 0; i < Call_Queue.size(); ++i)
{
std::cout << "Caller ID: " << Call_Queue[i]->Caller_ID << "\n";
}
I then ask the user to input an integer that will relate to the position in the call queue:
std::cin >> TheCall;
However, whenever I to remove a specific caller from the Queue by accessing a specific element and remove it, no matter what Data Type "TheCall" is, I get errors...
IntelliSense: no instance of overloaded function "std::deque<_Ty, _Alloc>::erase [with _Ty=Call_Center::Phone_Call *, _Alloc=std::allocator<Call_Center::Phone_Call *>]" matches the argument list
argument types are: (int)
object type is: std::deque<Call_Center::Phone_Call *, std::allocator<Call_Center::Phone_Call *>>
Ok, having read the C++ reference linked above I am now removing elements from the Call_Queue based on the Element that I have selected, however, is it possible to remove an Element based on the Caller_ID that it stores?
is it possible to remove an Element based on the Caller_ID that it stores?
There are several ways to do this :
You could just use a for loop to find the position of that element, and then use erase.
You could use std::find_if [1] (instead of a for loop) with a custom predicate [3] (this
can also be a lambda [4] if you are allowed to use C++11), and then, again, use erase.
You could use std::remove_if [2] with a custom predicate.
If you only want to erase an element in a specific position, you don't need a loop.
Just do this -> Call_Queue.erase(Call_Queue.begin() + TheCall);
If you want to erase one or more elements that have a specific Caller_ID, you can do something like this:
1 2 3 4 5 6 7
for (int i = 0; i < Call_Queue.size(); /* ++ i */)
{
if (Call_Queue[i]->Caller_ID != Caller_ID_To_Erase)
++ i; // only increase i if you don't erase
else
Call_Queue.erase(Call_Queue.begin() + i);
}
If you only want to erase an element in a specific position, you don't need a loop.
Just do this -> Call_Queue.erase(Call_Queue.begin() + TheCall);
I had this before, however this is still erasing the element associated with TheCall, this is not what I need,
for Example:
TheCall = 3 //The Call I want to erase
Caller 1 = Element [0]
Caller 2 = Element [1]
Caller 3 = Element [2]
Caller 4 = Element [3]
Caller 5 = Element [4]
If I erase the element associated with "TheCall", then it will be Element [3] (Caller 4) that is erased, not Element [2] (Caller 3), which is what I need.
If I erase the element associated with "TheCall", then it will be Element [3] (Caller 4) that is erased, not Element [2] (Caller 3), which is what I need.
Guess it takes some sort of genius to subtract one from a number.
I had a feeling someone was going to suggest this, however, in certain occasions, such as:
Caller 45 = Element[0]
Caller 158 = Element [77]
Caller 11 = Element [24]
etc...
Simply subtracting a 1 would not give me the result I need, but thank you for your sarcastic remark, much appreciated.
Thank you all for you help, m4ster r0chi, thank you for your help, the for loop you supplied was almost what was needed, just a few changes with the range of values & it is now working how I want it to.