delete element from list

A

arvind.nm

hi,
if i create a linked list like list<char> stk;
if i want to remove the first element from the list and i write
stk.pop_front() the node is still present but the char stored is now
'\0',why? how can i delete the first node. will
stk.erase(stk.begin()) work?
 
M

msalters

(e-mail address removed) schreef:
hi,
if i create a linked list like list<char> stk;
if i want to remove the first element from the list and i write
stk.pop_front() the node is still present but the char stored is now
'\0',why?

When you create the linked list like you do, it's empty. If you then
try to remove an element, you get Undefined Behavior. Anything is OK.
If on your compiler the result is size()==1 and front()==\0, then
that's what UB meant - on that occasion. Don't count on it, UB is
unpredictable in the extreme.

Solution: before removing the first element, check if it exists.
( !empty() )

HTH,
Michiel Salters
 
A

Andrew Koenig

if i create a linked list like list<char> stk;
if i want to remove the first element from the list and i write
stk.pop_front() the node is still present but the char stored is now
'\0',why? how can i delete the first node. will
stk.erase(stk.begin()) work?

Please show us some code that you think is not working correctly. It's hard
to make sense of your description of the problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,630
Members
45,334
Latest member
66Zeinab9

Latest Threads

Top