I am trying to represent a graph where
edge:
struct edge{
char a;
char b;
int weight;
}
I am trying to add my graph in this data structure:
vector<list<edge*>> graph;
In AddEdge function I get memory access violation while trying to add list in ith index of vector
void Graph::addEdge(char start, char end, int weight)
{
int i = node_number(start); //returns index (e.g 0 if start == 'A')
int j = node_number(end);
Edge *s= new Edge (start, end, weight);
Edge* e=new Edge (end, start, weight);
graph[i].push_back(s); //memory violation
graph[j].push_back(e);
}
Now someone help me to add edges in my graph
. Thanks!
EDIT:
I did debugging and the values of i and j are 0 and 1 respectively at the push_back() part. The debugger returns abort: memory violation the trace back is:
public:
_NODISCARD _Ty& operator[](const size_type _Pos)
{ // subscript mutable sequence
#if _ITERATOR_DEBUG_LEVEL != 0
_STL_VERIFY(_Pos < size(), "vector subscript out of range");
#endif /* _ITERATOR_DEBUG_LEVEL != 0 */
return (this->_Myfirst()[_Pos]);
}