typedef struct
{
int numerator;
int denominator;
} Fraction;
int main(void)
{
vector<Fraction> fractions = calcRatios(1000);
std::sort(
fractions.begin(),
fractions.end(),
[](Fraction a, Fraction b){
// Because of how the fractions are generated, 1/0 is in the vector
long double ld_a = (a.denominator != 0) ? a.numerator/a.denominator : __LDBL_MAX__,
ld_b = (b.denominator != 0) ? b.numerator/b.denominator : __LDBL_MAX__;
return ld_a < ld_b;
}
);
ofstream test("test.txt");
for(Fraction fraction: fractions)
test << setw(4) << setfill('0') << fraction.numerator << ",";
test << "n";
for(Fraction fraction: fractions)
test << setw(4) << setfill('0') << fraction.denominator << ",";
test.close();
}
I’m generating fractions, which are then sorted and printed into “test.txt.”
The screenshot above is test.txt. Row 1 are the numerators, Row 2 are the denominators. Each entry is padded with 0s to align them. As you can see on the boxed portions, they’re not actually sorted. What am I doing wrong?
1