Looping objects into Vector or Array

Feb 16, 2017 at 8:41pm
I have all these objects of type fraction over 100. Is there a way I can insert them into the vector with a loop?

The below code inserts the 1st object f1, but then the overloaded ++ operator gets called and I get f1 incremented by 1 and so on?


Fraction f1(5, 1);
Fraction f2(25, 50);
Fraction f3(4, 8);
Fraction f4(7,8);
Fraction f5(9, 1);
Fraction f6(2, 50);
Fraction f7(1, 98);
Fraction f8(5, 2);
Fraction f9(6,11);

Fraction key[9];
for(int i = 0; i < 9; ++i)
{
key[i] = f1++;

}

Feb 16, 2017 at 9:03pm
1
2
3
4
5
std::vector<Fraction> fractions;
fractions.emplace_back(5, 1);
fractions.emplace_back(25, 50);
fractions.emplace_back(4, 8);
//... 
Feb 16, 2017 at 9:12pm
Hi helios thanks for the reply. I was trying to insert with a loop as I would otherwise have to type out over 100 or close to 200 emplace_back commands?
Feb 16, 2017 at 9:35pm
Is there a pattern to the data? It doesn't look like there is in the code you've shown. How are you going to enter the data into the program without typing it in?
Feb 16, 2017 at 10:57pm
Thanks helios,

No there isn't, I created these Fractions then realized I needed to store them. I should have stored them during creation.
Topic archived. No new replies allowed.