The line
vector<int> list3(list1.size()+list2.size());
creates a vector of type int
and inserts list1.size()+list2.size()
default contructed elements. You want to create an empty vector of type int
and reserve memory for list1.size()+list2.size()
elements. Use
vector<int> list3;
list3.reserve(list1.size()+list2.size());
solved Some problem about the algorithm of merging two lists