问题描述
我做了一个测试与c + +并行quicksort程序如下第一列表为容器,然后我移动到一个通用的容器类型,但它报告了标题的错误。
I did a test with c++ parallel quicksort program as below first with list as container then I moved to a generic container type, but it reported the captioned error.
可以帮助这个吗?
#include <iostream> // std::cout
#include <future> // std::packaged_task, std::future
#include <chrono> // std::chrono::seconds
#include <thread> // std::thread, std::this_thread::sleep_for
#include <list>
#include <algorithm>
#include <type_traits>
#include <iterator>
template<typename F, typename A>
static std::future<typename std::result_of<F(A&&)>::type> spawn_task(F&& f, A&& a)
{
typedef typename std::result_of<F(A&&)>::type result_type;
std::packaged_task<result_type(A&&)> task(std::move(f));
std::future<result_type> res(task.get_future());
std::thread myThread(std::move(task), std::move(a));
myThread.detach();
return res;
}
template<class T, template<class T> class Container>
static Container<T> parallel_quick_sort(Container<T> input)
{
if (input.empty())
{
return input;
}
Container<T> result;
result.splice(result.begin(), input, input.begin());
T const& partition_val = *result.begin();
typename Container<T>::iterator divide_point = std::partition
(input.begin(), input.end(), [&](T const& t)
{
return t<partition_val;
}
);
Container<T> lower_part;
lower_part.splice(lower_part.end(), input, input.begin(), divide_point);
std::future<Container<T> > new_lower
(
spawn_task(¶llel_quick_sort<T>, std::move(lower_part))
);
Container<T> new_higher(parallel_quick_sort(std::move(input)));
result.splice(result.end(), new_higher);
result.splice(result.begin(), new_lower.get());
return result;
}
static void testQuickSort()
{
std::list<int> toSort={1, 4, 3, 6, 4, 89, 3};
std::for_each
(
std::begin(toSort), std::end(toSort), [](int n)
{
std::cout << n << std::endl;
}
);
std::list<int> sorted;
sorted = parallel_quick_sort(toSort);
std::for_each
(
std::begin(sorted), std::end(sorted), [](int n)
{
std::cout << n << std::endl;
}
);
}
错误讯息是:
../src/TestGenericQuickSort.h:67:41:错误:没有匹配函数调用'TestGenericQuickSort :: parallel_quick_sort(std :: list&)'sorted = parallel_quick_sort(toSort);
../src/TestGenericQuickSort.h:67:41: error: no matching function for call to ‘TestGenericQuickSort::parallel_quick_sort(std::list&)’ sorted=parallel_quick_sort(toSort);
../ src / TestGenericQuickSort.h:67:41:注意:候选人是:
../src/TestGenericQuickSort.h:67:41: note: candidate is:
../ src / TestGenericQuickSort.h:33: 22:note:template class Container> static Container TestGenericQuickSort :: parallel_quick_sort(Container)static Container parallel_quick_sort(Container input)
../src/TestGenericQuickSort.h:33:22: note: template class Container> static Container TestGenericQuickSort::parallel_quick_sort(Container) static Container parallel_quick_sort(Container input)
../ src / TestGenericQuickSort.h: 22:注意:模板参数推导/替换失败:
../src/TestGenericQuickSort.h:33:22: note: template argument deduction/substitution failed:
../ src / TestGenericQuickSort.h:67:41:错误:模板参数数量错误1)sorted = parallel_quick_sort(toSort);
../src/TestGenericQuickSort.h:67:41: error: wrong number of template arguments (2, should be 1) sorted=parallel_quick_sort(toSort);
../ src / TestGenericQuickSort.h:32:44:error: >
../src/TestGenericQuickSort.h:32:44: error: provided for ‘template class Container’ template class Container>
推荐答案
您传递 std :: list
,其完整声明是
template <typename T, typename Alloc = std::allocator<T> >
class list;
所以,它有2个模板参数,虽然秒一个有一个默认值为什么你看不到它)。
So, it has 2 template parameters, though the seconds one has a default value (and that's the reason why you don't see it).
更好的设计是传递2个输入迭代器和一个输出迭代器到你的函数:
A better design would be to pass 2 input iterators and an output iterator to your function:
template <typename IteratorIn, typename IteratorOut>
static OutputIterator parallel_quick_sort(IteratorIn begin, IteratorIn end, IteratorOut out);
有关详情,请参阅 std :: sort
对此签名。
See std::sort
for more details on this signature.
这篇关于C ++错误的模板参数数(2,应为1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!