Затем я попытался вывести значение в связанный список.
Вы пытались получить доступ к объекту, которого не было. Список adjacencyVector [i] не существует как смежность. Vector - пустой вектор, если вы не поместили в него элемент.
В следующем примере будут разъяснены некоторые детали.
#include <iostream>
#include <vector>
#include <list>
int main()
{
std::vector<std::list<int> > v; // This statement creates an empty vector of lists of integers.
std::list<int> l; // This statement creates an empty list of integers.
for(int i=0; i<10; ++i){
l.push_back(i);
v.push_back(l);
}
for(unsigned int i=0; i<v.size(); ++i){ // You can access elements calling the operator[].
for(std::list<int>::iterator it=v[i].begin(); it!=v[i].end(); ++it){
std::cout<<*it<<' '; // You can access elements calling the operator[] , you need an iterator.
}
std::cout<<std::endl;
}
//std::cout<<*(v[v.size()].begin())<<std::endl; // It causes segmentation fault, because v[10] does not exist,
// 10 is out of range of valid indexes.
}
Векторы и списки аналогичны. Вставка и удаление более эффективны, если вы используете список, но доступ к элементу обычно более эффективен, если вы используете вектор.
Какой размер? Это, по крайней мере, i + 1? – immibis