[C++-sig] On wrapping vector<pair<string, double> >

Nicodemus nicodemus at globalite.com.br
Tue Sep 30 13:06:31 CEST 2003


Zhang Le wrote:

>Hello, 
>  I'm trying to wrap a function that takes a vector<pair<string, double> >
>  as input and return a list. The original function is
>   vector<double> eval(const vector<pair<string, double> >&);
>
>   to use boost python, I write a wrapper function:
>   python::list py_eval(python::tuple input) {
>          here I want to use python::extract<std::string>(input[i][0])
>          and python::extract<double>(py_context[i][1])
>          to transform python input [('A', 1.0), ('B', 2.0), ...]
>          into a new vector<pair<string, double> > for calling real
>          eval()
>   }
>
>   Compilation is ok and I get an error in python code:
>   obj.py_eval([('A', 1.0), ('B', 2.0)])
>   TypeError: No registered converter was able to produce a C++ rvalue
>   of type j from this Python object of type str
>
>   I change the declaration to python::list input, python::object input
>   and get the same error result.
>
>   Am I missing anything?
>  
>

It is hard to tell by your description of the problem alone, some code 
would help. But try this:

py::list py_eval(py::list v)
{
    vector< pair<string, double> > input;
    int i;
    int len = py::extract<int>(v.attr("__len__")());
    for (i = 0; i < len; ++i) {
        string s = py::extract<string>(v[i][0]);
        double d = py::extract<double>(v[i][1]);
        input.push_back(pair<string, double>(s, d));
    }
    vector<double> result = eval(input);
    py::list pyresult;
    for (i = 0; i < result.size(); ++i) {
        pyresult.append(result[i]);               
    }
    return pyresult;
}                                 


HTH,
Nicodemus.





More information about the Cplusplus-sig mailing list