我如何使用迭代器访问元组中的数据

时间:2016-01-20 17:40:16

标签: c++ tuples

我在TheoryOfProgramming.com上找到了一些链表代码,我正在尝试编辑代码来打印元组而不是与迭代器配对。

是否可以在元组中声明成员变量(类似于对)?如果没有,我将如何使用迭代器来打印数据?

直接网址是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");
}

0 个答案:

没有答案