I have created a list and put some values into it. My list has its contents copied into an array, and then printed. My question that I have is, how do I delete the list that I have created?
Here is my code:
void main()
{
std::list<char> list;
while (1)
{
char in=0;
while(1)
{
scanf("%c",&in);
if(in=='\n') break;
list.push_back(in);
}
char* array=new char[list.size()]; // create a dynamic array
list<char> first;
std::copy(list.begin(),list.end(),array); // copy the data
for (int i=0 ; i<list.size() ; i++) printf("%c",array[i]);
printf("\nsize of array is: %d\n", list.size());
delete [] array; // destroy the dynamic array
}
}
Attempting to divine your meaning, I would guess that you want the list called ‘list’ to clear its contents after every iteration of the outer loop. list.clear() will do that, as will moving the declaration of list to within the outer loop. The destructor will properly free any resources you allocate and you will have a fresh instance on every iteration.