c++ - 向对向量添加元素会引发错误:分割违规信号
问题描述
我有以下课程:
namespace detail {
class AdjacencyListGraphBase {
protected:
// these are the edges in the graph - each outbound edge comes with a weight
std::vector<std::vector<std::pair<uint32_t, float>>> edges;
virtual size_t get_num_nodes() const = 0; // call the derived class to get the number of nodes
public:
void add_edge(uint32_t from, uint32_t to, float weight=1.0f);
void remove_edge(uint32_t from, uint32_t to);
std::optional<float> get_edge(uint32_t from, uint32_t to) const;
const std::vector<std::pair<uint32_t, float>>& get_edges_starting_at(uint32_t node) const;
};
}
我想添加一个具有以下功能的元素:
void detail::AdjacencyListGraphBase::add_edge(uint32_t from, uint32_t to, float weight)
{
if(!(edges[from][to].first==to)){
edges[from][to].first = to;
edges[from][to].second = weight;
}
}
但是当我调用此函数时,出现以下错误:SIGSEGV - Segmentation violation signal。我不知道我在这里做错了什么。我是否必须调整矢量边缘的大小?还是有什么我看不到的东西丢失了?提前致谢!
解决方案
推荐阅读
- ssl - 来自使用外部 IP 的家庭网络发往家庭服务器的 SSL 流量问题
- angularjs - 已解决 - 如何添加模糊角度过滤器
- python - 如何从两个组合的有序字典中提取单个值?
- java - openapi-generator /maven 插件生成带有 «, » 字符的无效 java 代码
- amazon-redshift - Redshift:每天更改表的名称
- css - 如何在 WKWebView 中使用 URL 加载 CSS 文件
- tmux - 哦,我的 zsh 在 tmux 上显示奇怪的空白区域
- c - fgets 总是在文件的第一行读取 3 个垃圾字符
- vb.net - vb.net 中的进度条
- ios - 显示 tableView 时,如何将 tableView 单元格排列为不同的顺序?