You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++ Vector成员未更新问题:邻接矩阵实现异常咨询

Troubleshooting Unchanged Adjacency Matrix Values in C++

It’s super frustrating when you think you’re modifying your adjacency matrix but the changes don’t show up—let’s break down the most likely culprits here, especially since you suspect pass-by-value issues.

1. Accidental Value Passing in Functions

Even if you think you’re using references, it’s easy to slip up and pass your vector<vector<int>> by value somewhere, which creates a copy. Any changes you make to that copy won’t affect the original matrix in your graph class.

Example of the Mistake:

Suppose your class has a member variable adj_matrix, and you have a helper function to modify it:

class graph {
private:
    vector<vector<int>> adj_matrix;

public:
    void add_edge(int u, int v, int weight) {
        // Passing adj_matrix by value here creates a copy
        update_edge(adj_matrix, u, v, weight);
    }

    // Helper function takes matrix by value—changes stay in the copy
    void update_edge(vector<vector<int>> mat, int u, int v, int w) {
        mat[u][v] = w;
    }
};

The Fix:

Pass the matrix by reference (using &) to ensure you’re modifying the original:

void update_edge(vector<vector<int>>& mat, int u, int v, int w) {
    mat[u][v] = w;
}

2. Uninitialized or Incorrectly Sized Matrix

If your adjacency matrix isn’t properly resized to match the number of vertices in your graph, accessing indices could lead to undefined behavior. You might be writing to memory that’s not actually part of the vector, so your changes don’t persist.

Example of the Mistake:

If you forget to initialize the matrix in your constructor:

graph(int num_vertices) {
    // Oops—no resize, so adj_matrix is empty
}

Trying to set adj_matrix[u][v] here would be out of bounds (and might crash or silently fail).

The Fix:

Resize the matrix to num_vertices x num_vertices (initialized to 0 or your default edge value) in the constructor:

graph(int num_vertices) {
    adj_matrix.resize(num_vertices, vector<int>(num_vertices, 0));
}

3. Variable Shadowing

If you declare a local vector<vector<int>> with the same name as your class’s member variable inside a function, you’ll be modifying the local copy instead of the class’s matrix.

Example of the Mistake:

void print_matrix() {
    vector<vector<int>> adj_matrix; // Local variable shadows the member
    // This prints the empty local matrix, not the class's data
    for (auto& row : adj_matrix) {
        for (int val : row) cout << val << " ";
        cout << endl;
    }
}

The Fix:

Either rename the local variable, or explicitly refer to the member using this->:

void print_matrix() {
    for (auto& row : this->adj_matrix) {
        for (int val : row) cout << val << " ";
        cout << endl;
    }
}

Quick Check List

  • Verify all functions modifying the matrix take it by reference.
  • Confirm your constructor initializes the matrix to the correct size.
  • Make sure you’re not shadowing the member variable with a local one.
  • Double-check that you’re printing the class’s adj_matrix member, not a copy.

If you share more of your implementation (like the add_edge method or constructor), we can pinpoint the exact issue—but these are the most common fixes for this problem.

内容的提问来源于stack exchange,提问作者cyber101

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:15:03