An elegant way to initialize an array of structures in C ++ 98

I am using gcc version 4.9.2 If I compile using the compiler flag -std = C ++ 0x, the following code compiles OK.

#include <iostream>
#include <vector>

using namespace std;
typedef struct
{
    vector<int> a;
    int b;
} MYTYPE;

int main(void)
{
    MYTYPE test[]=
    {
        { {1,2,3},4},
        { {5,6},7},
        { {},8}
    };
}

If I remove the -std = C ++ 0x flag, then the compiler reports:

error: failed to convert '{1, 2, 3} from' to 'std :: vector

What is an elegant way to initialize a test []?

+4
source share
3 answers

With C ++ 98, the best achievement would be to define and use a helper function:

struct Data
{
    std::vector<int> vector;
    int scalar;
};

template<std::size_t N>
Data make_data(const int (&vector)[N], int scalar)
{
    Data result;
    result.vector.assign(vector, vector+N);
    result.scalar = scalar;
    return result;
}

int main()
{
    const int vector[] = {1,2,3}; // just to annoy people using namespace std :)
    Data d = make_data(vector, 4);
    std::cout << d.vector[2] << "\n" << d.scalar << "\n";
}

live demonstration

+2
source

, struct, struct pre-++ 11.

++ 11.

+4

You can copy the solution from Boost.Assign.

Something like the code below - however, we can consider it "elegant" only if done once, used many times.

Purpose:

MYTYPE mm[] = {
    { VectorBuilder<int>(1)(2)(3), 4},
    { VectorBuilder<int>(3)(4)(5), 4}
};

Solution ( working example ):

template <typename T>
class VectorBuilder
{
public:
    VectorBuilder(T val) : val(val), prev(0), next(0), first(this)
    {}

    VectorBuilder operator ()(T val) const
    {
        return VectorBuilder(val, this);
    }

    operator std::vector<T>() const
    {
        std::vector<T> vv;
        vv.reserve(this->getSize());
        this->build(vv);
        return vv;
    }

private:

    T val;
    const VectorBuilder* prev;
    mutable const VectorBuilder* next;
    const VectorBuilder* first;

    VectorBuilder(T val, const VectorBuilder* prev) 
      : val(val), prev(prev), next(0), first(prev->first)
    {
        prev->next = this;
    }

    std::size_t getSize() const
    {
        // TODO - see working example link
    }

    void build(std::vector<T>& vv) const
    {
        // TODO - see working example link
    }

};
0
source

Source: https://habr.com/ru/post/1692543/


All Articles