Timeline
timeline
2025-09-27
init
two pointers
Title:
This problem can actually be merged in place, because the last n elements of num1 are 0, so you can merge from back to front. But the code below does not take this into account.
Note std::move, it actually converts the parameter t to an rvalue reference type, does not copy memory, does not release resources, does not call constructors or destructors
1 | template <typename T> |
Take std::vector
1 | std::vector<int> a = {1, 2, 3}; |
Here: std::move(a) turns a into an rvalue reference. The compiler will choose the vector’s move constructor instead of the copy constructor.
What the move constructor does:
- Directly ‘steal’ the internal pointer of a and give it to b.
- Set a’s pointer to null to avoid releasing the same memory during destruction.
So in the end: b holds the data of {1,2,3}. a becomes empty (size()==0, but is still a valid object)
Summary:
nums1 = std::move(vec);
- Old content is automatically released (done internally by move assignment)
- New content takes over
- vec becomes empty
No need to manually call the destructor.
1 |
|
