Remove specific element from Deque

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...

Please help!

Many thanks



whenever I to remove a specific caller from the Queue by accessing a specific element and remove it

How exactly are you doing that?

I get errors...

What errors do you get?

Have you seen this? -> http://www.cplusplus.com/reference/deque/deque/erase/
My last attempt was:
1
2
int TheCall;
Call_Queue.erase(TheCall);


I get error:

1
2
3
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 *>>
Last edited on
This is not how you should call std::deque::erase. Check the link I posted above for an example.
Last edited on
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.

[1] http://www.cplusplus.com/reference/algorithm/find_if/
[2] http://www.cplusplus.com/reference/algorithm/remove_if/
[3] http://www.cplusplus.com/forum/general/47440/#msg257688
[4] http://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11

EDIT : Note that remove_if will remove all elements satisfying your predicate.

EDIT2 : Oh, and you'll probably have to call erase too after
remove_if -> http://en.wikipedia.org/wiki/Erase-remove_idiom
Last edited on
I've tried a few of the suggestions that you have made, but I have had no luck as of yet...

The For Loop...

1
2
3
4
5
6
7
8
9
10
11
12
13
cin >> TheCall;

	for (int i = 0; i <= Call_Queue.size(); i++)
	{
		if (i != TheCall)
		{
			continue;
		}
		else
		{
			Erase The Caller!!
		}
	}


I just cant get the command right to erase the caller if it is found during the iteration of the loop, any suggestions?
Last edited on
erase takes an iterator. Supply one.

1
2
if ( theCall < Call_Queue.size() )
    Call_Queue.erase(Call_Queue.begin()+theCall) ;

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);
}
Last edited on
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.

Last edited on
Topic archived. No new replies allowed.