简体   繁体   English

合并排序C ++编译错误

[英]Merge Sort C++ Compile Error

I'm trying to implement a merge sort function and I'm getting a compiler error that says "no match for 'operator=' " It occurs on my recursive assignment of left_list = mergesort(left_list) and the same error in the following line using right_list. 我正在尝试实现合并排序功能,并且得到一个编译器错误,提示“与'operator ='不匹配”,这在我的left_list = mergesort(left_list)递归赋值和下一行相同的错误上发生使用right_list。

It compiles correctly if I take out the assignment and just have mergesort(left_list) but then it doesn't sort correctly. 如果我取出任务并只有mergesort(left_list),它可以正确编译,但是排序不正确。 I thought that the way I have it now should merge sort correctly, but given that it doesn't, either the error has something to do with those lines or it's elsewhere in the mergesort() or merge() function. 我以为我现在拥有的方式应该可以正确地合并排序,但是鉴于事实并非如此,错误可能与这些行有关,或者与mergesort()或merge()函数中的其他地方有关。

Any help would be appreciated. 任何帮助,将不胜感激。

void mergesort(vector<int> &data) {

vector<int> left_list;
vector<int> right_list;

if ( data.size() <= 1 ) {
    return;
}

// creates a middle point to separate into 2 sub lists
int middle =  ( data.size() / 2 );

// create a list of elements to the left of middle
for ( int i = 0; i < middle; i++ ) {
    left_list.push_back(data[i]);
}

// create a list of elements to the right of middle
for ( unsigned int i = middle; i < data.size(); i++ ) {
    right_list.push_back(data[i]);
}

// break down the sub lists until they are of size 1
left_list = mergesort(left_list);
right_list = mergesort(right_list);

// merge the sublists in the correct order
merge(left_list, right_list);



}

vector<int> merge(vector<int> &left, vector<int> &right) {

vector<int> result;

unsigned left_it = 0;
unsigned right_it = 0;

while( left_it < left.size() && right_it < right.size() ) {


    // the smaller value is put into the result vector
    if( left[left_it] < right[right_it] ) {

        result.push_back(left[left_it]);
        left_it++;
    }
    else
    {
        result.push_back( right[right_it] );
        right_it++;
    }
}

// Put the rest of the data from both vectors onto result
while( left_it < left.size() ) {

    result.push_back( left[left_it] );
    left_it++;
}

while( right_it < right.size() ) {

    result.push_back( right[right_it] );
    right_it++;
}

return result;

}

You have code that is trying to accept a return value from the mergesort function. 您有试图接受mergesort函数的返回值的mergesort If that is what you want, my original answer addresses that. 如果那是您想要的,我的原始答案可以解决。 However, if mergesort is supposed to update the input parameter with the sorted result, then it need not return any value, and void is fine. 但是,如果mergesort应该用排序的结果更新输入参数,则它不需要返回任何值,并且void是可以的。 Then, the assignment statements getting its return result is in error and should be changed. 然后,获取其返回结果的赋值语句是错误的,应进行更改。

mergesort(left_list);
mergesort(right_list);

However, the call to merge needs to assign the result to the input parameter. 但是, merge调用需要将结果分配给输入参数。

data = merge(left_list, right_list);

My original answer follows: 我的原始答案如下:

You should change your mergesort function to return the same type that merge returns. 您应该更改您的mergesort函数以返回与merge返回相同的类型。

vector<int> mergesort(vector<int> &data) {

You then need to update the mergesort implementation so that the first return statement returns the input parameter: 然后,您需要更新mergesort实现,以便第一个return语句返回输入参数:

return data;

The last statement should be changed to return the result: 最后一条语句应更改为返回结果:

return merge(left_list, right_list);

I haven't looked at the implementation of the algorithm itself. 我没有看算法本身的实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM