c++ - Converting void* to vector<int> -


i have function named deserialize takes input:

int(*cmp)(void*,void*) 

so, function of type can taken parameter function.

for example, if have point structure this:

typedef struct{     int x, y; }point; 

now, cmp function this:

int point_cmp(void* _p1, void* _p2){     point* p1=(point*)_p1;     point* p2=(point*)_p2;     return (!((p1->x==p2->x) && (p1->y==p2->y))); } 

this works.

but want vector.

i want write vector_cmp function can passed point_cmp deserialize. so, have tried thing it, wrong:

int vector_int_cmp(void* _v1, void* _v2){     vector<int> *v1 = vector<int> *_v1;     vector<int> *v2 = vector<int> *_v2;     auto diff = 0;     auto v1_it = v1->begin();     auto v2_it = v2->begin();     while(v1_it != v1->end() && v2_int != v2->end()){         if(*v1_it != *v2_it) diff++;         v1_it++;         v2_it++;    }     if(0 == diff && (v1_it != v1->end() || v2_it != v2->end())) diff = 1;    return diff; } 

what correct way this?

i suppose you're doing meet sort of external interface (which call function); in pure c++, there should never need this. anyhow:

int vector_compare( void const* p1, void const* p2 ) {     std::vector<int> const* v1 = static_cast<std::vector<int> const*>( p1 );     std::vector<int> const* v2 = static_cast<std::vector<int> const*>( p2 );     return *v1 < *v2         ? -1         : *v2 < *v1         ? 1         : 0; } 

should that's necessary.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -