我写了一个MergeSort
功能,这里是它的声明:如何使用一对迭代器[first,last)在模板中创建容器?
void MergeSort(
std::vector<int>::iterator first,
std::vector<int>::iterator last) {
// Get the same size as nums, so we can use some stable iteartors later.
std::vector<int> tmp_vector(last - first);
Sort(first, last, tmp_vector.begin());
std::copy(tmp_vector.begin(), tmp_vector.end(), first);
}
不过,我尝试使用模板,我发现,我不知道如何使用first
和last
创建tmp_vector
而我只知道类型:
template <typename RandomIterator>
void MergeSort(
RandomIterator first,
RandomIterator last) {
// Get the same size as nums, so we can use some stable iteartors later.
// How to create tmp_vector?
mergesort::Sort(first, last, tmp_vector.begin());
std::copy(tmp_vector.begin(), tmp_vector.end(), first);
}
据说it is not possible to get the type of the container of the iterators,所以任何想法?谢谢!
您需要首先将容器类型作为模板参数。 –
在迭代器后面甚至可能没有*一个容器。 –