I want put a value from front of the list into an another list but I am getting an error.
for exmaple
List<int> li;
List<int> li2;
.............
.............
li2.push_back(li.front()); // this statement doesnt work for me.
Can someone please help me.
example code:
list<int> li;
list<int> li2;
li.push_back(1);
li.push_back(2);
li.push_back(3);
for(int i=0;i<3;i++)
{
cout<<li.front()<<endl;
li2.push_back(li.pop_front());
}
Also, Be aware that when the list is empty, the pop_front() and pop_back() will throw exceptions – resulting in segmentation fault. So it is mandatory to check the size of the list
i. directly – using the list function
ii.indirectly using the for loop as shown in the program.
Sample Code
Ouput
-laptop:~/Study/Pgm$ ./test
Segmentation fault
I think this is the behaviour in all OS, I use g++ (4.4.1) on Linux platform.