tags:

views:

70

answers:

1

I am using matheval library. Its functions take c-style parameters, for example:

    #include<matheval.h>

    char * evaluator_evaluate(void * evaluator, int count, char **names, double *values);

In my case, I want to convert std::vector of names and std::vector of values to char ** and double *

Also, every name correspond to a unique value, and there could be repetitions. What is the best way to arrange names and values elegantly in terms of data structures? For now, I use vectors, and both contains repetitions.

Edit Please keep in mind that order of names and values matters and should match.

Thanks

+1  A: 

Internally, the standard requires that a vector<> is equivalent to an array. You can take the address of vector[0] and the resulting pointer will point to a contiguous area of memory where the data is stored, in the same order as the vector. This pointer is valid until or unless the vector<> is resized.

For std::string values (or any other object, for that matter), though, you will only get an array of string objects.

greyfade
greyfade, that nicely solves my first problem with doubles. What about the second? any suggestions?
All I can suggest is to allocate a char** array and initialize it with the string.c_str() pointers. That may cause problems if the C function expects to be able to change them, though.
greyfade
Perhaps, I can use another data structure. It's a fairly common problem -- I am sure there is a solution :)
Probably, but that is the only idea I have for strings. Good luck to you.
greyfade