'Seg fault when assigning a value to a 2D vector in C++
Ive been trying to change the values in this vector but keep getting stuck with a segmentation fault. I initialized the vector so i can get a rectangle of values in the vector, using zeros as fillers.
#include <bits/stdc++.h>
#include <vector>
using namespace std;
// vector initializer
void iniciaMat(vector<vector<int>> matAdj, int n, int m) {
for (int i = 0; i < n; i++) {
vector<int> linea;
for (int j = 0; j < m; j++) {
linea.push_back(0);
}
matAdj.push_back(linea);
}
}
void leeArcos(vector<vector<int>> matAdj, int m) {
int c, num1, num2;
char val1, val2;
for (int i = 0; i < m; i++) {
cin >> val1 >> val2 >> c;
num1 = val1 - 'A';
num2 = val2 - 'A';
matAdj[num1][num2] = c;
matAdj[num2][num1] = c;
}
}
void printVec(vector<vector<int>> matAdj, int n, int m) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << matAdj[i][j] << " " << endl;
}
cout << endl;
}
}
int main() {
// n is the number of nodes i.e. V
int n, m;
cin >> n >> m;
vector<vector<int>> matAdj;
iniciaMat(matAdj, n, m);
leeArcos(matAdj, m);
printVec(matAdj, n, m);
return 0;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
