-3

What would be the right way to handle a scenario when you want to return a vector<vector<int>> say named as triplets vector<vector<int>> triplets;. There are two possible returns involved. First, when nothing is populated in triplets as condition to populate it was never satisfied. Second depending on a scenario it is populated with vector having 3 elements.

if(nums[left_index]+nums[i]+nums[right_index] == 0)
                    {
                        triplets[total_count].push_back(nums[left_index]);
                        triplets[total_count].push_back(nums[i]);
                        triplets[total_count].push_back(nums[right_index]);
                        ++total_count;
                    }

Above is a scenario taken from code posted below.

    class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        int total_size=nums.size();
        vector<vector<int>> triplets;
        if(total_size<3)
        {
            triplets;
        }
        
        std::sort(nums.begin(),nums.end());
        int total_count = 0;
        for(int i = 1; i<total_size-1;++i)
        {
            
            int right_index = i+1;
            int left_index = 0;
            std::cout << i <<" " <<left_index <<" " << right_index <<"\n";
            while(right_index <= total_size-1)
            {
                while(left_index < i)
                {
                    std::cout << i <<" " <<left_index <<" " << right_index <<"\n";
                    std::cout <<"Sums is:" << nums[left_index]+nums[i]+nums[right_index] <<"\n";
                    if(nums[left_index]+nums[i]+nums[right_index] > 0)
                    {
                        break;
                    }
                    if(nums[left_index]+nums[i]+nums[right_index] == 0)
                    {
                        triplets[total_count].push_back(nums[left_index]);
                        triplets[total_count].push_back(nums[i]);
                        triplets[total_count].push_back(nums[right_index]);
                        ++total_count;
                    }
                    left_index++;
                }
                ++right_index;
            }
        }
        return triplets;
    }
};

The error that I receive while running my code is :

Line 1034: Char 9: runtime error: reference binding to null pointer of type 'std::vector<int, std::allocator<int>>' (stl_vector.h)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:1043:9

Is it possible to create a reserve for such scenarios when vector of vector is involved. How would reserve be done for inner and outer vector ?

Invictus
  • 4,028
  • 10
  • 50
  • 80
  • 2
    @Invictus `class Solution {` That means leetcode I think? To be honest I wouldn't be surprised to learn that some people downvote just for that. – john Aug 09 '23 at 16:10
  • 2
    Downvote might be related to the fact this is clearly a "competitive coding" excercise, and not a strict question about C++ (the language). The code for competitive coding solutions is often hard to read and/or it is clear bad C++ programming habits have crept in. Like hard to read code with unclear variable names and missing small functions (with clear names). So yes Leetcode has a kind of a bad reputation here – Pepijn Kramer Aug 09 '23 at 16:11
  • The advise we often give is to first learn C++, so you will also can better interpret the error messages you are seeing and why. – Pepijn Kramer Aug 09 '23 at 16:14
  • 3
    So good sources to learn cpp from are : A [recent C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or have a go at https://www.learncpp.com/ (that's pretty decent, and pretty up-to-date). For C++ reference material use : [cppreference](https://en.cppreference.com/w/). And after you learned the C++ basics from those sources, look at the [C++ coreguidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines) regularely to keep up-to-date with the latest guidelines. – Pepijn Kramer Aug 09 '23 at 16:14
  • 2
    @Invictus Since this is leetcode, you should post a `main` program with the test case that fails, and the `main` simply creating an instance of `Solution` with the test data. Your error is a runtime error, and missing these details could cause downvotes to occur. Another reason for leetcode downvotes many times is a lack of debugging, and explicitly because Leetcode gives the failing test cases to the participant, but the participant fails to do any debugging. Not saying you are guilty of this, but just to let you know what can cause a downvote on Leetcode-ish questions. – PaulMcKenzie Aug 09 '23 at 16:24
  • 1
    This question's code/phrasing very likely came from one of many countless coding challenge/puzzle websites. They take advantage of people who want to learn C++ by offering coding puzzles based on arcane knowledge or programming tricks; combined with a claim that solving those useless coding puzzles makes anyone a C++ expert. This is untrue, of course, but these coding puzzles (that have no learning or real-world value) are unsolvable without knowing certain, arcane tricks. Everyone eventually figures out this scam, but only after a long time, with nothing to show for it. – Sam Varshavchik Aug 09 '23 at 16:39

1 Answers1

2

The problem is that your triplets vector has a size of zero when you try to assign triplets to it. You understood that you need to use push_back on the second dimension of your 2D vector, but forgot that the first dimension also needs push_back.

There's a few different ways you could handle this, I'd perhaps go with something like this

triplets.push_back(std::vector<int>(3, 0)); // add a triplet of zeros
triplets.back()[0] = nums[left_index];      // assign to the triplet just added
triplets.back()[1] = nums[i];
triplets.back()[2] = nums[right_index];

Also it seems that

if(total_size<3)
{
    triplets;
}

should be

if(total_size<3)
{
    return triplets;
}
john
  • 85,011
  • 4
  • 57
  • 81