模板不接受C+中的参数+; 我学习C++,请原谅我这个问题,但我已经有相当长的一段时间了。
我尝试使用插入排序对向量进行部分排序 以下是函数:模板不接受C+中的参数+; 我学习C++,请原谅我这个问题,但我已经有相当长的一段时间了。,c++,templates,C++,Templates,我尝试使用插入排序对向量进行部分排序 以下是函数: template <typename T> // template, typename: pg 419 void insertion_sort (T *a, long int n) { for (long int i = 1; i < n; i++) { for (long int j = i; (j > 0) && (a[j] < a[j-1]); j--) { std:
template <typename T> // template, typename: pg 419
void insertion_sort (T *a, long int n) {
for (long int i = 1; i < n; i++) {
for (long int j = i; (j > 0) && (a[j] < a[j-1]); j--) {
std::swap(a[j], a[j-1]); // swap: pg 1294, 1297
}
}
}
template//template,类型名称:pg 419
空插入_排序(T*a,long int n){
用于(长整数i=1;i0)和&(a[j]
以下是我的合并函数代码:
template <typename T> void sort(vector<T> &a, vector<T> & aux, long int low, long int high) {
long int size = high - low + 1;
if (size < INSERTION_THRESHOLD) {
insertion_sort(a[low], (long int) a.size() - low);
}
else {
if (high <= low) {
return;
}
long int mid = low + (high - low)/2;
sort(a, aux, low, mid);
sort(a, aux, mid + 1, high);
merge(a, aux, low , mid, high);
}
}
模板无效排序(向量和a、向量和辅助、长整型低、长整型高){
长整数大小=高-低+1;
如果(大小<插入\u阈值){
插入_排序(a[low],(long int)a.size()-low);
}
否则{
if(高插入排序(&a[low],…);
a[low]
是对元素的引用。你的插入排序
需要一个指针。此外,插入排序
的第二个参数不应该是你从高
和低
计算出来的大小吗?@reima谢谢你抓住了!我有一个问题。在C中,a[n]应该是指针吗?那么a[n]如果a是一个向量,那它就是一个参考?
sort.cpp: In instantiation of ‘void sort(std::vector<T>&, std::vector<T>&, long int, long int) [with T = double]’:
sort.cpp:147:82: required from here
sort.cpp:71:51: error: no matching function for call to ‘insertion_sort(__gnu_cxx::__alloc_traits<std::allocator<double> >::value_type&, long int)’
insertion_sort(a[low], (long int) a.size() - low);
^
sort.cpp:71:51: note: candidates are:
In file included from sort.cpp:6:0:
sort.hpp:11:28: note: template<class T> void insertion_sort(T*, long int)
template <typename T> void insertion_sort (T *a, long int n)