简体   繁体   中英

c++ understanding the syntax of templates, typename

I am new to c++ but I have enough Java knowledge from the past to understand the concept of generics; I have been a python dev for many years now.

I am learning c++ by examples and I came across this code for a generics implementation of merge sort .

template<typename I>
void doMerge(I begin, I midPoint, I end)
{
    typename std::vector<typename std::iterator_traits<I>::value_type> TmpVec;

    TmpVec tmp(std::make_move_iterator(begin), std::make_move_iterator(end));

    TmpVec::iterator   beginAlt   = std::begin(tmp);
    TmpVec::iterator   endAlt     = std::end(tmp);
    TmpVec::iterator   midAlt     = std::next(beginAlt, std::distance(begin, midPoint));


    TmpVec::iterator   l   = beginAlt
    TmpVec::iterator   r   = midAlt;
    I                  i   = begin;

    while(l < midAlt && r < endAlt)
    {
        *i = std::move((*l < *r) ? *l++ : *r++);
        ++i;
    }
    while(l < midAlt)
    {   *i  = std::move(*l++);
        ++i;
    }
    while(r < endAlt)
    {   *i  = std::move(*r++);
        ++i;
    }
}
template<typename I>
void mergeSort(I begin, I end)
{
    std::size_t length  = std::distance(begin, end);
    if (length <= 1)
    {   return;
    }

    std::size_t mid      = length/2;
    I           midPoint = std::next(begin, mid);

    mergeSort(begin, midPoint);
    mergeSort(midPoint, end);

    doMerge(begin, midPoint, end);
}

I am using g++ to compile from a Makefile with the following command

g++ -std=c++98 merge_sort.cpp -o mergesort.out

merge_sort.cpp:34:11: error: expected ';' after expression
    TmpVec tmp(std::make_move_iterator(begin), std::make_move_iterator(end));

Can someone explain the compile error and provide some insight into the following parts:

template<typename I>
void doMerge(I begin, I midPoint, I end)

The template keyword allows the function to accept a generic Iterator, correct? But why can I not define one global template in a header file to use in all functions in this code file?

在C ++ 11中添加了std::make_move_iterator() ,因此您无法使用-std=c++98编译

Thanks to @NathanOliver for his comment. The below compiles and executes correctly:

template<typename I>
void doMerge(I begin, I midPoint, I end)
{
    typedef std::vector<typename std::iterator_traits<I>::value_type> TmpVec;

    TmpVec tmp(std::make_move_iterator(begin), std::make_move_iterator(end));

    typename TmpVec::iterator   beginAlt   = std::begin(tmp);
    typename TmpVec::iterator   endAlt     = std::end(tmp);
    typename TmpVec::iterator   midAlt     = std::next(beginAlt, std::distance(begin, midPoint));


    typename TmpVec::iterator   l   = beginAlt;
    typename TmpVec::iterator   r   = midAlt;
    I                  i   = begin;

    while(l < midAlt && r < endAlt)
    {
        *i = std::move((*l < *r) ? *l++ : *r++);
        ++i;
    }
    while(l < midAlt)
    {   *i  = std::move(*l++);
        ++i;
    }
    while(r < endAlt)
    {   *i  = std::move(*r++);
        ++i;
    }
}
template<typename I>
void mergeSort(I begin, I end)
{
    std::size_t length  = std::distance(begin, end);
    if (length <= 1)
    {   return;
    }

    std::size_t mid      = length/2;
    I           midPoint = std::next(begin, mid);

    mergeSort(begin, midPoint);
    mergeSort(midPoint, end);

    doMerge(begin, midPoint, end);
}


int main()
{
    std::vector<int>    data  {{ 5,12,45,2,67,8}};
    mergeSort(std::begin(data), std::end(data));

    std::copy(std::begin(data), std::end(data), std::ostream_iterator<int>(std::cout, ", "));
    std::cout << "\n";
}

Thanks to @TriskalJM for the c++11 tip!

$ make
rm -rf *.out
g++ -std=c++11 merge_sort.cpp -o mergesort.out
./mergesort.out
2, 5, 8, 12, 45, 67, 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM