0

I found some linked list code on TheoryOfProgramming.com, I'm trying to edit the code to print a tuple instead of a pair with an iterator.

Is it possible to declare member variables in a tuple (similar to pairs)? If not, how would I would use an iterator to print the data?

The direct url is here http://theoryofprogramming.com/adjacency-list-using-cpp-stl/

vector< list< tuple<int, int,int> > > adjacencyList(vertices + 1);

printf("Enter the Edges V1 -> V2, of weight W\n");

for (int i = 1; i <= edges; ++i) {
    scanf("%d%d%d,%d", &v1, &v2, &weight,&time);

    // Adding Edge to the Directed Graph
    adjacencyList[v1].push_back(make_tuple(v2, weight,time));
}

printf("\nThe Adjacency List-\n");
// Printing Adjacency List
for (int i = 1; i < adjacencyList.size(); ++i) {
    printf("adjacencyList[%d] ", i);

    list< tuple<int, int,int> >::iterator itr = adjacencyList[i].begin();

    while (itr != adjacencyList[i].end()) {
        printf(" -> %d(%d)", (*itr).first, (*itr).second,(*itr).third);
        ++itr;
    }
    printf("\n");
}
  • You cannot. Tuple values need to be resolved at compile time. – πάντα ῥεῖ Jan 20 '16 at 17:44
  • maybe [`std::get`](http://en.cppreference.com/w/cpp/utility/tuple/get) is what you want. – 101010 Jan 20 '16 at 17:44
  • @101010 Still not feasible to provide kind of `iterator` based access at runtime. – πάντα ῥεῖ Jan 20 '16 at 17:46
  • @πάνταῥεῖ I believe that tuple types are resolved at compile time not values, or am I wrong? As for the `iterator`, I believe you can code a custom iterator to iterate over a tuple, provided of-course that all its types are the same. – 101010 Jan 20 '16 at 17:48
  • 2
    @101010 With uniform types may be. But why not simply use a `std::array` then instead? – πάντα ῥεῖ Jan 20 '16 at 18:03
  • @πάνταῥεῖ I agree with you, in terms of simplicity a `std::array` should be used on this context, my response however is based on the OP's inquiry. – 101010 Jan 20 '16 at 18:17

0 Answers0