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 ?
 
    