Search code examples
c++arraysalgorithmstructstdlist

How to copy elements from std::list to an array of struct?


I need to copy the contents of a std::list into an array, wherein the array is struct of array. Below is the code implementation of it.

#include <iostream>
#include <string>
using namespace std;

typedef struct
{
    int height;
    int width;
    int length;
}dimensions;
GetDimensions(list<std::string>, *int); // Function that copies the content of list to array passed as second parameter

int main() 
{
    dimensions cuboid[10];
    int plane[10];

    list<std::string> planeList = GetList();//Function that returns list of elements
    list<std::string> dimensionList = GetList();

    GetDimensions(planeList,&plane);//This is fine, as it is a simple array
    GetDimensions(dimensionList,&cuboid.height);//Trouble in implementation of this usecase, for cuboid.height, cuboid.width and cuboid.height.
    return 0;
}

GetDimensions(list<std::string>dimensionList, int* dimensionParams)
{
    int i=0;
    for(list<std::string>::iterator it = dimensionList.begin(); it != dimensionList.end(); ++it)
    {
        dimensionParams[i] = stoi(*it);
        i++;
    }
}

Here, I need GetDimensions() function to copy the list (passed as first parameter) to array (second parameter). The implemented function works well for simple array plane. But how to pass the array of struct as parameter to the function ?

I will be getting the std::list as cuboid.height, cuboid.width and cuboid.length. So the function has to copy the contents of list from cuboid[0].height to cuboid[i].height respectively. Is there any specific function to copy the content directly?


Solution

  • You can do this with boost::transform_iterator.

    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <functional>
    #include <boost/iterator/transform_iterator.hpp>
    
    struct dimensions {
        int height;
        int width;
        int length;
    };
    
    template <typename OutputIt>
    void GetDimensions(std::list<std::string> dimensionList, OutputIt dimensionParams)
    {
        // N.b. taking the address of a standard library function is undefined, so wrap in a lambda
        auto stoi = [](std::string s){ return std::stoi(s); };
    
        std::copy(boost::make_transform_iterator(dimensionList.begin(), stoi),
            boost::make_transform_iterator(dimensionList.end(), stoi), 
            dimensionParams);
    }
    
    int main() {
        dimensions cuboid[10];
        int plane[10];
    
        std::list<std::string> planeList = GetList();
        std::list<std::string> heightList = GetList();
        std::list<std::string> widthList = GetList();
        std::list<std::string> lengthList = GetList();
    
        GetDimensions(planeList, plane);
        GetDimensions(heightList, 
            boost::make_transform_iterator(cuboid, std::mem_fn(&dimensions::height)));
        GetDimensions(widthList, 
            boost::make_transform_iterator(cuboid, std::mem_fn(&dimensions::width)));
        GetDimensions(lengthList, 
            boost::make_transform_iterator(cuboid, std::mem_fn(&dimensions::length)));
        return 0;
    }