vector sort with templates problem

can't seem to understand the error message or find the error. the error message i get is:

D:\codeblocks\sort with template\main.cpp|10|error: cannot convert '__gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >' to 'double*' in initialization|
in line 10 and 19 below:

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
45
46
47
  #include <iostream>
#include <vector>
using namespace std;

template<typename type>
void sort(vector<type> &vec)
{
    for(typename vector<type>::iterator itr=vec.begin(),end=vec.end();itr!=end;++itr)
    {
        type *ptr_to_lowest_value=itr;
        for(typename vector<type>::iterator second_itr=(itr+1);second_itr!=end;++itr)
        {
            if(*itr<*second_itr)
            {
                //do nothing
            }
            else if(*itr>*second_itr)
            {
                ptr_to_lowest_value=second_itr;

            }

        }
        if(*itr!=*ptr_to_lowest_value)//check with jjust memory address
        {
            type temp=*itr;
            *itr=*ptr_to_lowest_value;
            *ptr_to_lowest_value=temp;
        }
        else
        {//do nothing}
    }


}
}

int main()
{
    vector<double> vec;
    vec.push_back(3);
    vec.push_back(2);
    vec.push_back(5);
    vec.push_back(7);
    vec.push_back(4);
    sort<double> (vec);
}

any help would be aprreciated!
Last edited on
vector<type>::iterator and type* is not the same type so you can't assign one to the other. You are using iterators everywhere else so why not let ptr_to_lowest_value be an iterator too?
Last edited on
i don't get it. i am iterating over a vector that contains data of certain type(vector<type>::iterator) and ptr_to_lowest_value also points to the same type. it appears consistent, so why
you can't assign one to the other
. please explain.

also i am using iterator at other places and ptr_to_lowest_value is just a temp medium to store the lowest value encountered so far. the task of iteration is done by other iterators.
They both "point" to the same type but they are not the same type.

If you want to get a pointer from the iterator you can first dereference the iterator and then use the address-of operator to get a pointer to the object.

 
type* ptr_to_lowest_value = &*itr;

Or you could change the type (and name) of ptr_to_lowest_value to an iterator which will allow you to assign the iterator directly.

 
typename vector<type>::iterator itr_to_lowest_value = itr;

Last edited on
i quite lost you there.
They both "point" to the same type but they are not the same type.
the book i am using(jumping into c++) did not get into much detail, so i didn't quite understand that. can you name what 'actual' type they are.

also i quite understood both your solution. though, the second way you advised about using iterator, how can we use that if we don't actually use that to iterate over vector( and just to store the temporary value to store the lowest value so far)
If you declare a pointer like this:

 
int *ptr;

the type of ptr is int* (pointer to int). That is why you often see many people write the * closer to the type being pointed to, like this:

 
int* ptr;

The meaning is the same so it's just a matter of taste.

vector<int>::iterator is the iterator type for vector<int>. The iterator can be used much like a pointer but usually it's not a pointer, but some other class type.

Iterators are designed to be similar to pointers because it allows us to write template functions that work for many different containers, and we can even use them on arrays by using pointers as iterators.

1
2
3
4
5
6
7
// sort vector elements
std::vector<int> vec{5, 3, 1};
std::sort(vec.begin(), vec.end());
    
// sort array elements
int arr[] = {5, 3, 1};
std::sort(arr, arr + 3); // Note that here we are using int* as iterators 

More about iterators: http://www.cplusplus.com/reference/iterator/
i got what you said in last post but, one part i ain't clear about what you said in your second-last post.

1
2
typename vector<type>::iterator itr=vec.begin()   ///1
type *ptr_to_lowest_value=itr;   ///2 


you said
They both "point" to the same type but they are not the same type.

ok, they both point to same type, that's what i thought and that's why i assigned one to the other but if they aren't the same type, can you precisely say what type they actually are?
Topic archived. No new replies allowed.