Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
787 views
in Technique[技术] by (71.8m points)

c++ - runtime error: reference binding to null pointer of type 'int' (merge function)

I am trying to solve the problem of finding the median of two sorted arrays on Leetcode but I am getting the following error: Line 811: Char 16: runtime error: reference binding to null pointer of type 'int' (stl_iterator.h). Can someone help me solve this error?

class Solution {
      public:
          double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
               vector<int>nums3;
               merge(nums1.begin(),nums2.end(),nums2.begin(),nums2.end(),nums3.begin());
               
               double median;
               int size_of_merged_vec=nums3.size();
               
               if (size_of_merged_vec%2==0){
                   median = nums3[size_of_merged_vec/2-1]+ nums3[size_of_merged_vec/2];
               }   
               else{
                   median = nums3[size_of_merged_vec/2-1];
               }
              return median;
         }

};

question from:https://stackoverflow.com/questions/65904463/runtime-error-reference-binding-to-null-pointer-of-type-int-merge-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Declare num3 as vector<int>nums3(nums1.size() + nums2.size());. Also in your merge second parameter needs to be nums1.end()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...