在 c++ 中访问列表向量时出现问题
Problem in accessing vectors of list in c++
>我正在尝试表示一个图形,其中
边缘:
struct edge{
char a;
char b;
int weight;
}
我正在尝试在此数据结构中添加我的图形:
vector<list<edge*>> graph;
在AddEdge函数中,我在尝试在向量的第i个索引中添加列表时遇到内存访问冲突
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);
}
现在有人帮我在graph
中添加边缘.谢谢!
编辑:
我进行了调试,i 和 j 的值在 push_back(( 部分分别为 0 和 1。调试器返回中止:内存冲突 回溯为:
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]);
}
问题出在向量的大小上,因为下面的初始化分配 size=0
vector<list<edge*>> graph;
我已经通过在推送之前调整图形向量的大小来修复代码。 另一种解决方案是通过以下方式给出初始大小
vector<list<edge*>> graph(20);
相关文章:
- 如何在C++向量中奇数元素前面加上值-1,我在使用insert函数时遇到了问题
- 添加存储在向量中的大整数的函数出现问题
- 尝试将向量初始化为类成员时出现问题
- 在将字符串放入字符串向量时遇到问题?
- 取消引用向量时出现问题
- 在C++中对结构向量进行排序时出现问题
- 在 c++ 中创建和访问 4D 向量时出现问题
- 数组为此合并排序函数提供了正确的输出,但向量给出了不正确的输出.出了什么问题?
- 使用指针向量(带代码)C++(以及对其使用的便利性的怀疑)时出现问题
- C++ 继承向量问题(无限循环+在其他类中使用向量的问题)
- 在 c++ 中访问列表向量时出现问题
- c++向量push_back问题
- shared_ptr类的向量的向量初始化问题
- SWIG c++到python:向量问题
- 在 Rcpp-Function 中使用布尔向量的子集(Rcpp 初学者的问题..)
- 无法取消引用超出范围的向量迭代器 - 有什么问题?
- 在构造函数中更新指针向量时出现问题
- 在使用 std::sort() 和 lambda 函数按属性对 ADT 的向量进行排序时遇到问题
- 为什么C++在自定义结构向量时会出现 ostream 重载问题?
- C++:问题向量 STL.