C++部分排序错误
C++ Partial Sorting Errors
我想做的是生成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
,但从不使用它。最好从代码中删除这样的行来简化它。
- C++选择排序算法中的逻辑错误
- 在c++中尝试对对象数组进行排序时,出现std:bad_alloc错误
- 显示错误输出的简单数组排序程序
- 对单向链表进行排序时出现运行时错误
- 无法在我的堆排序代码中找到错误.无法正确执行.C++
- 在气泡排序程序中未声明错误功能
- 仅在大型阵列上出现合并排序分段错误
- 分段 排序函数实现中的错误
- C++运行时错误与快速排序算法抛出堆栈转储错误
- 字符串上的合并排序上的 Seg 错误
- 将图形表示为unordered_map<字符串、向量>时拓扑排序错误<string>
- C++为什么我的指针选择排序中存在分段错误?
- 我想在 c++ 中对单向链表进行冒泡排序,但我一直面临左值错误,无法解决它
- 尝试按值对地图进行排序,但出现错误
- 使用快速排序对 C++ 中的可视化工具错误进行排序
- 合并排序:分段错误核心转储
- 比较迭代器会使程序崩溃,而不会在自定义气泡排序实现中出现错误
- 合并排序中的错误计数:计数反转
- 使用STL对用户输入数组进行排序的错误有什么解决方案吗?
- C++中的错误排序算法