How to print a list vector (a pointer to a list)

0

Hello, I would like to know how I can print a pointer to a list, as a list vector. I made a pointer pointing to list and I do not know how to print those values. I tried using an iterator for each position of the vector and printing from start to end of the list, but I could not. It sounds simple, but I can not do it. Does anyone know a way to do this? Here is the code:

#include <cstdlib>
#include <stdlib.h>
#include <stdio.h>
#include <list>
#include<iostream>

using namespace std;

int main(int argc, char** argv) {

    list<int> *adjV;
    list<int>::iterator it;

    adjV[0].push_back(10);
    adjV[0].push_front(20);
    adjV[0].push_back(30);

    adjV[1].push_back(45);
    adjV[1].push_front(55);
    adjV[1].push_back(65);

    adjV[2].push_back(80);
    adjV[2].push_front(90);
    adjV[2].push_back(100);

    for (it = adjV[0].begin(); it != adjV[0].end(); it++)
        cout << *it << endl;

    for (it = adjV[1].begin(); it != adjV[1].end(); it++)
        cout << *it << endl;

    for (it = adjV[2].begin(); it != adjV[2].end(); it++)
        cout << *it << endl;


    return 0;
}

A segmentation fault (core dumped) occurs when it executes.

    
asked by anonymous 13.03.2018 / 23:55

1 answer

1

List vector initialization is missing:

list<int> *adjV = new list<int>[3];

With this initialization you get the expected result.

However, you can now take advantage of and make writing in just 2 aliased to simplify and make it more flexible:

for(int i = 0; i < 3; ++i) {
    for (list<int>::iterator it = adjV[i].begin(); it!= adjV[i].end(); ++it){
        cout<<*it<<endl;
    }
}

See this Ideone solution

    
14.03.2018 / 00:16