-4

Sorry but this is a quick question, can anyone tell why this for loop is not incrementing either i or j? They are staying at 0 and 1.

void Environment::tourneyselection(std::vector<Tree>& popvec) {
    std:random_shuffle(popvec.begin(), popvec.end());
    for (int i = 0, j = 1; j <= Popsize; i + 2, j + 2) {
        std::cout << popvec[i].fitness << " and " << popvec[j].fitness << ":";
        if (popvec[i].fitness < popvec[j].fitness) {
            popvec.erase(popvec.begin() + i);
            std::cout << " erase " << i << std::endl;
        }
        else {
            popvec.erase(popvec.begin() + j);
            std::cout << " erase " << j << std::endl;
        }

    }
}

Thank you

nbstrong
  • 115
  • 1
  • 10
  • 3
    `j+2` doesn't do anything useful. Try `j+=2`. – Jean-François Fabre Oct 15 '16 at 21:19
  • `i + 2, j + 2` are NOPs actually. use `i += 2, j += 2)` instead. – πάντα ῥεῖ Oct 15 '16 at 21:20
  • That seems to produce errors. ; i += 2, j += 2) Edit: Oh sorry no my debugger had just frozen up – nbstrong Oct 15 '16 at 21:20
  • @MushinZero Which ones specifically? – πάντα ῥεῖ Oct 15 '16 at 21:21
  • Nvm, that worked. Now I am getting vector out of range but I see that's because it eventually becomes smaller than iterator range. I am going to see if I cannot make the values empty and then resize the vector after. – nbstrong Oct 15 '16 at 21:23
  • @MushinZero Please stop asking that kind of questions at Stack Overflow. There's no value for future research, and we're not your personal help desk. Get a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) about c++ and learn from there in 1st place. – πάντα ῥεῖ Oct 15 '16 at 21:29
  • Isn't asking questions the point? I have a couple books on C++ but I had just overlooked this. People forget things all the time. – nbstrong Oct 15 '16 at 21:43
  • @MushinZero Read [here](http://stackoverflow.com/help/dont-ask) and [here](http://stackoverflow.com/help/how-to-ask) please. Then think twice before posting again. – πάντα ῥεῖ Oct 15 '16 at 21:59
  • The first link is an explanation to not post subjective questions, which this is not. The second is how to write a good question. While this is a simple question, it does not appear to break the rules that would categorize it as a bad question. Yes a little more searching would have probably helped, but I did google the question first and because it was overlooking a simple issue the highest answers were for deeper more complex problems with for loops than something simple like this and did not apply. – nbstrong Oct 15 '16 at 22:10

2 Answers2

2
j + 2

You're not modifying j at all. You wanted to say

j += 2

The same thing goes for i + 2.

krzaq
  • 16,240
  • 4
  • 46
  • 61
1

Neither i + 2 nor j + 2 are making an assignment, you're performing calculations but not assigning the result to either of i or j

Try i += 2 and j += 2

Alternatively, if you want to keep it simple or just break it down a bit - try i = (i + 2) and j = (j + 2)

the += operator allows you to perform the calculation while at the same time, assign the result.