I am a newbie in programming. Whenever I include vectors in my programming ,compiler shows segmentation fault. Can anyone help me where am I wrong? And I strongly doubt that it is due to vectors since after commenting out vectors part program runs well. I thought it might be the issue with FOLLOWING IS THE CODE
#include <climits>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int a, b; // a rows b columns
cin >> a >> b;
vector<vector<int>> k(a, vector<int>(b));
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
int ele;
cin >> ele;
k[i].push_back(ele);
}
}
vector<vector<int>> ans(a, vector<int>(b));
for (int count = 0; count < a * b; count++) {
int j2 = count % b, i2 = count / b;
if (count % 4 == 0) { // left to right
int q = k[0].size();
while (q > 0) {
ans[i2].push_back(k[0][0]);
k[0].erase(k[0].begin());
q--;
}
}
if (count % 4 == 1) { // up to down
int q = 0;
while (q < k.size()) {
ans[i2].push_back(k[q][k[0].size() - 1]);
k[q].pop_back();
q++;
}
}
if (count % 4 == 2) { // right to left
int q = k[0].size();
while (q > 0) {
ans[i2].push_back(k[k.size() - 1][q - 1]);
k[k.size() - 1].pop_back();
q--;
}
}
if (count % 4 == 3) { // down to up
int q = k.size();
while (q > 0) {
ans[i2].push_back(k[q - 1][0]);
k[q - 1].erase(k[q - 1].begin());
q--;
}
}
}
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
cout << ans[a][b];
}
}
return 0;
}