C++部分排序错误

C++ Partial Sorting Errors

本文关键字:错误 排序 C++      更新时间:2023-10-16

我想做的是生成n个几乎排序的值。

例如,用户将输入值1000。

然后,它将通过调用部分排序函数来传递排序作业。

基本上,我只会对总值的一半进行排序。

也就是说,在已经生成的1000个中。。仅对前一半或500个值进行排序。

排序完成后,将所有接近排序的值推送到一个向量中。

然而,我在编译过程中遇到了一些错误,我不明白这意味着什么。有人能帮我做这些吗?谢谢

以下2个错误:

1) "partial_sort":对重载函数c:\users\mk\documents\visual studio 2013\projects\algorithm analysis\analgorithm analysis\nearlysorted.cpp 49 1 algorithm analysis 的调用不明确

2) IntelliSense:重载函数"partial_sort"的多个实例与参数列表匹配:函数模板"void partial_sort(随机访问开始,随机访问排序,随机访问结束)"函数模板"void std::partial_sort(_RanIt _First,_RanIt _Mid,_RanIt _Last)"参数类型为:(std::_Vector_titerator>>,std::-Vector_t迭代器>>,std::_Vector _titerator>>)c:\Users\Mk\Documents\Visual Studio 2013\Projects\Algorithm Analysis\Agorithm Analysis \nearlysorted.cpp 49 2算法分析

代码:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
template <class rand_access>//class
void partial_sort(
    rand_access  begin,
    rand_access  sort,
    rand_access  end
    );
template <class rand_access, class BinaryPred>//overloading
void partial_sort(
    rand_access  begin,
    rand_access  sort,
    rand_access  end,
    BinaryPred comp
    );
//Function prototype
//void decrease_store(int val, vector<int> &aVec); TODO SOON
void nearlystd_store(int val, vector<int> &aVec);

int main()
{
    int nvalue;
    vector<int> int_vector;
    cout << "How many numbers would you like to generate?n";
    cin >> nvalue;//get input from user
    nearlystd_store(nvalue, int_vector);// pass user input to the function
    system("pause");
    return 0;
}
void nearlystd_store(int val, vector<int> &aVec)//nearly sorted function
{
    vector<int>::iterator Iter;// a vector
    int num;
    for (int i = 0; i < val; i ++)//generate from the start till desired nvalue
    {
        aVec.push_back(val - i);//push into this vector
    }
    partial_sort(aVec.begin(), aVec.begin() + (val / 2), aVec.end());//sort half in the vector
    cout << "The Output:n";
    for (Iter = aVec.begin(); Iter != aVec.end(); ++Iter)//push sorted value
    {
        cout << *Iter << " " << endl;//aVec.push_back(int()); --- Not sure if correct
    }
}

编辑代码:

感谢llya&Chris帮助

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
//Function prototype
//void decrease_store(int val, vector<int> &aVec); TODO SOON
void nearlystd_store(int val, vector<int> &aVec);

int main()
{
    int nvalue;
    vector<int> int_vector;
    cout << "How many numbers would you like to generate?n";
    cin >> nvalue;//get input from user
    nearlystd_store(nvalue, int_vector);// pass user input to the function
    system("pause");
    return 0;
}
void nearlystd_store(int val, vector<int> &aVec)//nearly sorted function
{
    vector<int>::iterator Iter;// a vector
    for (int i = 0; i < val; i ++)//generate from the start till desired nvalue
    {
        aVec.push_back(val - i);//push into this vector
    }
    partial_sort(aVec.begin(), aVec.begin() + (val / 2), aVec.end());//sort half in the vector
    cout << "The Output:n";
    for (Iter = aVec.begin(); Iter != aVec.end(); ++Iter)//push sorted value
    {
        cout << *Iter << " " << endl;//aVec.push_back(int()); --- Not sure if correct
    }
}

让我们看看编译错误(下次在问题中添加它!):

prog.cpp: In function ‘void nearlystd_store(int, std::vector<int>&)’:
prog.cpp:49:68: error: call of overloaded ‘partial_sort(std::vector<int>::iterator, __gnu_cxx::__normal_iterator<int*, std::vector<int> >, std::vector<int>::iterator)’ is ambiguous
     partial_sort(aVec.begin(), aVec.begin() + (val / 2), aVec.end());//sort half in the vector
                                                                    ^
prog.cpp:49:68: note: candidates are:
prog.cpp:8:6: note: void partial_sort(rand_access, rand_access, rand_access) [with rand_access = __gnu_cxx::__normal_iterator<int*, std::vector<int> >]
 void partial_sort(
      ^
In file included from /usr/include/c++/4.8/algorithm:62:0,
                 from prog.cpp:3:
/usr/include/c++/4.8/bits/stl_algo.h:5308:5: note: void std::partial_sort(_RAIter, _RAIter, _RAIter) [with _RAIter = __gnu_cxx::__normal_iterator<int*, std::vector<int> >]
     partial_sort(_RandomAccessIterator __first,
     ^

在这里,编译器通知您,很难决定必须调用partial_sort的哪个实现。有两种可能的选择,所以编译器不能确定哪一种是正确的。你需要避免这种不确定性。在这种情况下,您可以重命名错误的声明(即,将"template//class"中的所有行删除为"//Function prototype")。

prog.cpp:43:9: warning: unused variable ‘num’ [-Wunused-variable]
     int num;
         ^

这只是警告。在这里,您声明了变量num,但从不使用它。最好从代码中删除这样的行来简化它。