我想要做的是生成n个近似排序的值。
例如,用户将输入nvalue 1000。
然后通过调用部分排序函数来传递排序作业。
基本上,我只会对总值的一半进行排序。
意思是说,在生成的1000个中,只有前半部分或500个值才会被排序。
完成排序后,它会将所有近似排序的值推送到矢量中。
但是,我遇到了一些错误,我无法理解它在编译过程中意味着什么。有人可以帮我这些吗?谢谢
以下2个错误:
1)'partial_sort':模糊调用重载函数c:\ users \ mk \ documents \ visual studio 2013 \ projects \ algorithm analysis \ algorithm analysis \ almostsorted.cpp 49 1算法分析
2)IntelliSense:多个重载函数实例“partial_sort”与参数列表匹配: 函数模板“void partial_sort(rand_access begin,rand_access sort,rand_access end)” 函数模板“void std :: partial_sort(_RanIt _First,_RanIt _Mid,_RanIt _Last)” 参数类型是:(std :: _ Vector_iterator>>,std :: _ Vector_iterator>>,std :: _ Vector_iterator>>)c:\ Users \ Mk \ Documents \ Visual Studio 2013 \ Projects \ Algorithm Analysis \ Algorithm Analysis \ almostsorted.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&amp;克里斯帮助
#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
}
}
答案 0 :(得分:0)
让我们看看编译错误(下次将其添加到问题中!):
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
,但从不使用它。最好从代码中删除这些行以简化它。