vendredi 26 novembre 2021

Boost transformed conversion to vector error

I am new to using boost adapters, I am using the following code for conversion of a vector one class to transformed version.

The return type of boost::transformed is not as per expectation. Can someone please illuminate as to what I am missing here:

class blabla
{
    public:
    int x;
};

class blabla2
{
    public:
    int  y;
    blabla2(int a)
    {
        y=a;
    }
};


int main()
{
    using namespace boost::adaptors;
    std::vector<blabla> test;

    auto foo = [](const blabla& A) -> std::pair<blabla2, double> {
        return std::make_pair(blabla2(A.x), double(A.x));
    };

    const auto histogram = test | boost::adaptors::transformed(foo);
    // std::vector<std::pair<blabla2, double>> should be return type? 

    std::vector<std::pair<blabla2, double>> histogramtest = histogram; ----> this line gives error unexpectedly. Why is it so?
    std::pair<blabla2, double> x = histogram[0];
}

The line std::vector<std::pair<blabla2, double>> histogramtest = histogram; gives error

While std::pair<blabla2, double> x = histogram[0]; works correctly. Why is that so?

Aucun commentaire:

Enregistrer un commentaire