i try relatively simple task - create a vector and sort it. But unfortunately when destroying the vector from stack, it fails with SIGTRAP. When I remove the sort, its OK, but i rather need it.
I have narrowed the issue to smallest possible source code. When I run it, i get windows error "Program stopped unexpectedly". When i use GDB attached, it highlights the vector destructor after finishing the first while loop. (See picture below)
I have tried both vector<Vec> veci(n);
and resize(n)
. I sense it could be caused by the sort algorithm somehow sorting items not present in the vector, but the code doesnt indicate the problem... do you see it?
Windows 10, mingw gcc
#include <stdio.h>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <ctime>
#include <stack>
#include <vector>
#include <algorithm>
#include <numeric>
#include <string>
#include <sstream>
using namespace std;
struct Vec {
int cena;
int vaha;
float pomer;
bool operator < (const Vec& oth) const
{
return !(pomer < oth.pomer);
}
};
int main(int argc, char** argv) {
stringstream file;
file << "9550 40 600 14 223 38 230 3 54 1 214 13 118 4 147 15 16 2 104 5 56 49 154 40 106 24 234 18 34 33 195 7 74 10 129 12 159 42 37 41 10 11 185 6 243 45 87 32 57 20 87 9 26 16 201 39 0 23 128 39 194 21 10 46 1 8 28 30 59 26 130 35 160 22 91 34 180 19 16 31 1 17 72";
while (file.good()) {
int id;
int n;
int M;
file >> id;
file >> n;
file >> M;
vector<Vec> veci;
veci.resize(n);
for (int i = 0; i < n; i++) {
int c, v;
file >> v;
file >> c;
veci[i].vaha = v;
veci[i].cena = c;
veci[i].pomer = c / (v+1);
}
sort(veci.begin(), veci.end());
}
printf("end");
return 0;
}