Rashid Ellis
Rashid Ellis

Reputation: 330

Converting a struct to an array

As programming becomes more complex, and the need to perform operations on struct data becomes visible. Is there a conversion method for converting a struct type into an array of its members such that:

struct FooDesc Foo{
    int num_Foo;
    int num_Bar;
    int GreenFoo;
};

can be represented by:

int Bar[2];

Or better, dynamically as:

vector<int> Bar;

The goal is to convert or re-represent the data struct as an iteratable form, without the excessive use of the assignment operator.

Upvotes: 3

Views: 4921

Answers (3)

sfjac
sfjac

Reputation: 7294

If your structs are POD then you might consider using std::tuple instead of structs. You could then use various template facilities to work through the members of the tuple.

Here is a simple example that prints the elements of a tuple - using boost::fusion::tuple instead of the std::tuple since it has many more tuple-manipulating facilities available:

#include <boost/fusion/tuple.hpp>
#include <boost/fusion/include/for_each.hpp>
#include <iostream>

struct Printer {    
    template<typename T>
    void operator()(const T &t) const {
        std::cout << t << std::endl;
    }
};

int main(int argc, const char * argv[])
{
    boost::fusion::tuple<int, int, int, int, float> t = 
          boost::fusion::make_tuple(3, 5, 1, 9, 7.6f);

    boost::fusion::for_each(t, Printer());

    return 0;
}

You could include these in unions with the struct but you'd want to do some testing to ensure proper alignment agreement.

The upside is that these manipulations are very fast - most of the work is done at compile time. The down-side is that you can't use normal control structs like indexing with runtime indices - you'd have to build an abstraction layer around that as the normal get<i>(tuple) accessor requires that i be a compile time constant. Whether this is worth the complexity depends strongly on the application.

Upvotes: 2

Jonathan
Jonathan

Reputation: 1100

How about:

vector <Foo> Bar;

You can then add instances of your struct and then access each element as desired, using an array-like format.

See this related question for further information:

Vector of structs initialization

Upon re-reading your question a few times, I think I mis-understood your intent and answered the "wrong question". You can make an array of your struct as mentioned above and index it as an array, but I don't believe it is quite as simple as that to make each struct element a different element of an array. If you are looking to make an array of structs, my answer should help. If you are looking to make each element of your struct an element of your array, 40two's answer should help you out.

Upvotes: 1

Dimitrios Bouzas
Dimitrios Bouzas

Reputation: 42899

You could use unnamed structs to make a hybrid struct where its member could be treated as an array:

struct  Foo {
  union {
    struct {
      int x;
      int y;
      int z;
    };
    struct {
      int array[3];
    };
  };
};

LIVE DEMO

Note however, that unnamed struct comes from C11 and its not a standard C++ feature. It is supported as an extension though by GCC as well Clang.

Upvotes: 6

Related Questions