3

I have a following class

template <class T>
class Graph {
    private:
        std::vector < std::vector<T> > data;
    public:
        class Edge {
            private:
                T source;
                T destination;
        };
};

I will be trying to model digraph using edge-centric approach (https://stackoverflow.com/a/2157012/1864702).

I want to be able to construct objects of this class in a following way:

enum Country {Poland, Ukraine, Germany, USA};
typedef Graph<Country> GC;

GC gc{
    { Poland, {Ukraine, Germany} },
    { Germany, {Poland, Ukraine, USA} },
    { USA, {Poland, Ukraine, USA} }
};

What kind of arguments should constructor of class Graph take to allow such a syntax?

1 Answer 1

4

You can add a helper class into Graph and take an initializer list of these helper structs:

template <class T>
class Graph {
    private:
        std::vector < std::vector<T> > data;
        struct EdgeConstructor
        {
            T source;
            std::vector<T> destination;
        };
    public:
        class Edge {
            private:
                T source;
                T destination;
        };
        Graph(std::initializer_list<EdgeConstructor>) {}
};

Live example

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.