C++ LIS解决方案中的分段错误,用于破解编码访问11.7

C++ LIS解决方案中的分段错误,用于破解编码访问11.7,c++,algorithm,segmentation-fault,lis,C++,Algorithm,Segmentation Fault,Lis,破解编码面试时出现问题-第五部分问题11.7 我在这里尝试的解决方案是java中的解决方案,转换成C++。但是我面临的是分割错误的问题。p> #include<iostream> #include<vector> #include<algorithm> using namespace std; class HtWt { public: int ht; int wt; HtWt(int ht,int wt

破解编码面试时出现问题-第五部分问题11.7 我在这里尝试的解决方案是java中的解决方案,转换成C++。但是我面临的是分割错误的问题。p>
#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;
class HtWt
{
    public:
        int ht;
        int wt;
        HtWt(int ht,int wt):ht(ht),wt(wt){}
        HtWt(const HtWt &other)
        {
            this->ht=other.ht;
            this->wt=other.wt;
        }
        bool operator<(const HtWt& obj) const
        {
            cout << __func__ << std::endl;
            return (this->ht<obj.ht && this->wt<obj.wt);
        }
};

typedef vector<HtWt> vHtWt;
typedef vector<vHtWt > vvHtWt;

vHtWt& getSeqWithMaxLen(vHtWt& seq1,vHtWt& seq2)
{
    cout << __func__ << std::endl;
    if(seq1.empty())
        return seq2;
    if(seq2.empty())
        return seq1;
    return (seq1.size() > seq2.size() ? seq1 : seq2);
}

void  LIS(vHtWt& arr,vvHtWt& solutions,int current_index)
{
    cout << __func__ << std::endl;
    if(current_index>arr.size()-1 || current_index<0)
        return;
    cout<<"arr.size()="<<arr.size()<<"current_index = "<<current_index<<endl;
    HtWt cur_element = arr[current_index];
    /* Find longest sequence we can append current_element to */
    vHtWt best_sequence;
    for(int i=0;i<current_index;i++)
    {
        cout<<"inside for loop"<<endl;
        if (arr[i]<cur_element)
            best_sequence = getSeqWithMaxLen(best_sequence,solutions[i]);
            //cout<<"{"<<best_sequence[best_sequence.size()-1].ht<<","<<best_sequence[best_sequence.size()-1].wt<<"}"<<" ";
    }
    /* Append current_element */
    vHtWt new_solution;
    if(!best_sequence.empty())
        new_solution.insert(new_solution.end(),best_sequence.begin(),best_sequence.end());
    new_solution.push_back(cur_element);  
    /* Add to list and recurse */
    solutions[current_index] = new_solution;
    LIS(arr,solutions,current_index+1);
}

vHtWt LIS(vHtWt& arr)
{
    cout << __func__ << std::endl;
    vvHtWt solutions;
    LIS(arr,solutions,0);
    vHtWt best_sequence;
    for(int i=0;i<arr.size();i++)
        best_sequence = getSeqWithMaxLen(best_sequence,solutions[i]);
    return best_sequence;
}

vHtWt getLIS(vHtWt& arr)
{
    cout << __func__ << std::endl;
    // sort the array for either height or weight 
    sort(arr.begin(),arr.end());
    return LIS(arr);
}

int main()
{
    HtWt arr[] = {HtWt(12, 13), HtWt(11, 15), HtWt(9, 20), HtWt(20, 20), HtWt(40, 21), HtWt(8, 42)};
    vHtWt vArr(arr,arr+(sizeof(arr)/sizeof(arr[0])));
    vHtWt result = getLIS(vArr);
    for(int i=0;i<result.size();i++)
        cout<<"{"<<result[i].ht<<","<<result[i].wt<<"}"<<" ";
    cout<<endl;
    return 0;
}
arr的大小是6,索引是0,所以我想应该没有分段错误

$ ./a.out 
getLIS
operator<
operator<
operator<
operator<
operator<
LIS
LIS
arr.size()=6current_index = 0
Segmentation fault: 11
$/a.out
盖特利斯
接线员<
接线员<
接线员<
接线员<
接线员<
利斯
利斯
阵列大小()=6当前索引=0
分段错误:11

您在第行没有遇到分段错误

HtWt cur_元素=arr[当前_索引]但在

solutions[当前_索引]=新的_解决方案

因为
解决方案
尚未初始化。换乘线路

vvHtWt解决方案

vvHtWt解决方案(arr.size())

它看起来像堆栈溢出(因为涉及递归)。你能把bt贴在这里吗?想看看堆栈深度,即帧数。
$ ./a.out 
getLIS
operator<
operator<
operator<
operator<
operator<
LIS
LIS
arr.size()=6current_index = 0
Segmentation fault: 11