STL - List
Nov 28, 2010 at 3:49pm UTC
The code is for STL - List. May I know what is 'splice' and how to get the answers as 0 & 6 respectively? Tks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int > list1;
list<int > list2;
list1.push_back(10);
list1.push_back(20);
list1.push_back(30);
list2.push_back(1);
list2.push_back(2);
list2.push_back(3);
list<int >::iterator pos = list1.begin();
list1.splice(pos, list2);
cout << "Size of list 2 is:" << list2.size() << endl; //Ans is 0
cout << "Size of list 1 is:" << list1.size() << endl; //Ans is 6
}
Nov 28, 2010 at 4:17pm UTC
Splice is neat.
list::splice( iterator position, list<T,Allocator>& x );
...
Moves elements from list x into the list container at the specified position, effectively inserting the specified elements into the container and removing them from x.
More here:
http://cplusplus.com/reference/stl/list/splice/
-Albatross
Nov 28, 2010 at 11:17pm UTC
Ok. I just noticed that I am looking at the output size. List2 size is empty & List1 size is 6 due to elements from List2 moves over to List1.
Tks Albatross.
Topic archived. No new replies allowed.