Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 437
Sathiya Avatar asked Mar 04 '23 13:03

Sathiya


2 Answers

Use std::array 's instead. Then your problem can be reduced to passing two different types of arrays to a single function.

This can be solved

  • either by good old function overloads
  • or in c++17 function template with if-constexpr.

Following is an example code with templated function with if-constexpr (See live online)

#include <iostream>
#include <string>
#include <list>
#include <array>
#include <type_traits>  // std::is_same_v

struct dimensions // no need to typedef here
{
        int height;
        int width;
        int length;
};

template<typename T>
void GetDimensions(const list<std::string>& dimensionList, T& dimensionParams) 
^^^^               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //---> pass list by const-ref as the values are non-modifying
{
    int i{0};
    if constexpr (std::is_same_v<std::array<int, 10>, T>)
    {
        for(const std::string& str: dimensionList)   dimensionParams[i++] = std::stoi(str);
    } 
    else
    {
        for(const std::string& str: dimensionList) dimensionParams[i++].height = std::stoi(str);
    }
}

int main()
 {
    std::array<dimensions, 10> cuboid;  // use std::array instead of VLA
    std::array<int, 10> plane;

    std::list<std::string> planeList{"1", "2"}; // some list
    std::list<std::string> dimensionList{"1", "2"};

    GetDimensions(planeList, plane);
    GetDimensions(dimensionList, cuboid);
    return 0;
}

Also note that:

  • You have not specified the return type of GetDimensions function. You probably want to return void there.
  • in C++ you do not need to use typedef alias for struct { ... }.

  • last but not least, do not practice with using namespace std;

like image 152
JeJo Avatar answered Mar 16 '23 19:03

JeJo


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;
}
like image 32
Caleth Avatar answered Mar 16 '23 17:03

Caleth