The second version (template function), has the same behavior, but takes a specific function to perform the comparison operation in charge of determining the insertion points. The comparison function has to perform weak strict ordering (which basically means the comparison operation has to be transitive and irreflexive).
The merging is performed using two iterators: one to iterate through x and another one to keep the insertion point in the list object; During the iteration of x, if the current element in x compares less than the element at the current insertion point in the list object, the element is removed from x and inserted into that location, otherwise the insertion point is advanced. This operation is repeated until either end is reached, in which moment the remaining elements of x (if any) are moved to the end of the list object and the function returns (this last operation is performed in constant time).
The entire operation does not involve the construction or destruction of any element object.
If no ordering is required, another option for merging lists is member function list::splice, which is faster.
Parameters
- x
- A list object containing the same type of objects as this container.
- comp
- Comparison function that, taking two values of the same type than those contained in the list object, returns true if the first argument is less than the second, and false otherwise.
Return value
noneExample
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
Output:
first contains: 1.4 2.2 2.9 2.1 3.1 3.7 7.1 |
Notice how in the second merger, the function mycomparison (which only compares the integral parts) did not consider 2.1 lower than 2.2 or 2.9, so it was inserted right after them, before 3.1.
Complexity
At most, linear in x.size() + this->size() - 1.See also
| list::splice | Move elements from list to list (public member function) |
| list::insert | Insert elements (public member function) |
| list::remove | Remove elements with specific value (public member function) |
| list::erase | Erase elements (public member function) |
| list::push_back | Add element at the end (public member function) |
