What's the difference between copy and uninitialized_copy?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//What's the difference between copy and uninitialized_copy?
////Why can't I replace uninitialized_copy with copy here.
   // std::uninitialized_copy( first, last, newFirst );


template <typename T >
typename T::value_type MostFreq( T first, T last )
{
    std::size_t amount = 0;
    T start = first;
    while ( start != last )
    {
        amount++;
        start++;
    }

    typedef std::vector<typename T::value_type>  VecType;

    VecType vec(amount);
    typename VecType::iterator newFirst = vec.begin();
    typename VecType::iterator newLast = vec.end();
//Why can't I replace uninitialized_copy with copy here.
    std::uninitialized_copy( first, last, newFirst );


    while( newFirst != newLast )
    {
        {
            {
            }
            occu = 0;
        }
        ++occu;
        preIter = newFirst;
        ++newFirst;
    }
    if ( occu > maxOccu )
    {
        maxOccu = occu;
        maxOccuElement = preIter;
    }

    return *maxOccuElement;
}
What are you doing or want to do ? , the function have the logical errors ...
This is an example I see in a book.
I want to know the difference between copy and uninitialized_copy.
You can and should replace uninitialized_copy with copy in this particular circumstance.

uninitialized_copy constructs objects at the destination, but there are already objects constructed at the destination in vec.
Topic archived. No new replies allowed.