0

can someone help me to solve this error

Line 786: Char 17: runtime error: reference binding to null pointer of type 'int' (stl_iterator.h)

class Solution {
public:
    vector<int> findDisappearedNumbers(vector<int>& nums) {
        vector<int>result;
        sort(nums.begin(),nums.end());
        int p=1;
        int minel=*min_element(nums.begin(),nums.end());
        int maxa=*max_element(nums.begin(),nums.end());
        for(int64_t i=minel;i<=maxa;i++)
        {
            int c=count(nums.begin(),nums.end(),i);
            if(c==0)
            {
                result.push_back(i);
            }
        }
        return result;
    }
};
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
suraj
  • 69
  • 9
  • code is giving run time error as reference binding to null pointer of type 'int' (stl_iterator.h) – suraj Feb 28 '20 at 10:25
  • Please include the complete error message in the question. It should contain eg the line number (that you will have to show us because line number here might be different from yours) and a [mcve] would also be good – 463035818_is_not_an_ai Feb 28 '20 at 10:26
  • done you might check it now – suraj Feb 28 '20 at 10:28
  • 1
    `int minel=*min_element(nums.begin(),nums.end());` why don't you check if `nums` is not empty? – rafix07 Feb 28 '20 at 10:29
  • there is no line 786 in the code you posted. Sorry, for insisting, but I really want to help you getting an answer. The problem is very likely related to what rafix mentioned, but to be really sure a [mcve] would be required and the line the error points to. – 463035818_is_not_an_ai Feb 28 '20 at 10:30
  • actually line 786 is of c++ stl library – suraj Feb 28 '20 at 10:37
  • The **complete** error message should also contain information on which line in your code causes the errror – 463035818_is_not_an_ai Feb 28 '20 at 10:38
  • btw the STL is not the same as the C++ standard library. removed the tag. see here: https://stackoverflow.com/questions/5205491/whats-the-difference-between-stl-and-c-standard-library – 463035818_is_not_an_ai Feb 28 '20 at 10:51

1 Answers1

1

Things can fail and when you use something that can fail you should check if it did or not. In particular std::max_element returns...

Iterator to the greatest element in the range [first, last). If several elements in the range are equivalent to the greatest element, returns the iterator to the first such element. Returns last if the range is empty.

You should change this

int minel=*min_element(nums.begin(),nums.end());

to

auto it = min_element(nums.begin(),nums.end());
if (it != nums.end()) {
    auto minel = *it;
}  else {
    // do not use it
}

Alternatively check if nums is empty once at the beginning of the function. The code seems to be for a online contest. Check the requirements, if an empty input is valid input you need to handle it. If empty vector is not a requirement you have to handle you might have a bug in code that you did not show.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185