1

I want to return an alternate to NULL for the vector as vectors can't be NULL also I don't want to return an empty vector its already returned in another condition.

std::vector<int> how_sum(long target_sum, std::vector<int> numbers, std::map<int, std::vector<int>> &memo)
{
    . . .

    if (target_sum == 0)
        return {};    // empty vector returned here

    if (target_sum < 0)
        // return an alternate to NULL vector

    . . .
}
Sidharth Mudgil
  • 1,293
  • 8
  • 25
  • 3
    How can a `std::vector` be `NULL` ? – paolo Aug 05 '22 at 14:56
  • `NULL` is a scalar value, `std::vector` is a class, they are not compatible. – The Dreams Wind Aug 05 '22 at 14:56
  • A `std::vector` is not a pointer, and can never be `NULL`. It also cannot be the integer value `0`. Are you trying to return an empty vector? – Nathan Pierson Aug 05 '22 at 14:56
  • @paolo then what should I return instead of NULL? – Sidharth Mudgil Aug 05 '22 at 14:57
  • 1
    I'm probably missing something here - why ca't you just return the empty vector? I would even simplify the code: at the end, always `return res`. You can replace the existing `reeturn` statement with a `break`. – MSalters Aug 15 '22 at 05:53
  • @MSalters because I already returned an empty vector when target_sum is 0; Actually I solved the problem by returning a vector containing -1 since in this problem vector can only contain positive numbers solved my problem but I can't think of any other solution, Thanks I'll simplify code by `break` – Sidharth Mudgil Aug 15 '22 at 06:32
  • The `target_sum<0` looks like a `std::invalid_argument` exception at first glance, but hen you would want to avoid calling `how_sum` recursively when `target_sum-sum` is negative. This is a standard thing with recursive functions: you often want a non-recursive wrapper to deal with error checking etcetera, parts that are needed only once and not recursively – MSalters Aug 15 '22 at 06:50
  • 1
    Return `std::optional>`. Or throw an exception on failure. – HolyBlackCat Oct 01 '22 at 12:27

0 Answers0