I am having some issues around iterating and incrementing through a vector, both it's element and it's values. What I am trying to do in this mess below is take the first and second elements of a vector, compare them and then swap then if one value is less then the other. I am getting two main errors, one "out of range memory location" and the other was that I can't increment a vector iterator. Could anyone point me in the right direction here? Sorry for the messy code, I've been at this for a while and when trough a number of different possibilies.
Your main function is missing a closing brace and the "count" variable in the swap function is not declared. I don't know about the "can't increment a vector iterator" error. Could you post the error exactly as it is shown?
Disregard the syntax in main. I cut it up for this post. Assuming main is correct. Can what I am trying to do in the swap function be done as it is here. Yes you are correct in saying that I want to check more then the first two elements. The idea would be to check all elements from i+1 to the end of the array against the first element (i). Then move the smallest (or largest value to the beining of the vector) and continue to do so until the elements are ordered.
The compiler doesn't like me iterating the vector iterator (++i), but I figure out why the I am getting the memory out of range. The for loop condition needs to be less one (eg. grade->end()-1).
Is the compiler only giving you a warning for saying ++i? If so, ignore it, that is the standard way to iterate over a vector. Like I said, the out of range (assuming you have at least two elements in the vector) is happening because your second for loop does not have a terminating condition.
I got passed the errors and compiler issues. When it compiles the vector isn't iterating properly. idx + 1 is adding one to the value instead of moving to the next element. How do I access the vector so that I can move element positions? Same with the if statement it's comparing element i (say 45) to i + 1 (46)
It gives you the last element in the vector. You don't need the -1 in that case, though, because you'd never get to the last grade at all.
I have a question: why do you need to use vector iterators anyway?
Can't you just use for(vector<int>::size_type i = 0; i < MyVector.size(); ++i) and access the elements in the vector with the subscript [] operator?
You don't need to, but if you switch to another container that doesn't support subscript random access, then you won't need to rewrite the code to use iterators.
Ok so this one compiles with no issues. However it is looping to many times. For 3 inputs, it loops 6 times when on the 4 loop its correct. How do I contain the nested loops.
So how would you then compare two elements in a vector.
(ie)
Input 45, 85, 95.
// check for equality among all input elements. See if 45 is equal 85, then 95. then check to see if 85 is equal to 95. I tried to use a nest loop as in my previous post but it does work properly.
Well, you would iterate from the first to the second last element. First, set a bool (allEqual) to true. In each iteration, check if *itr == *(itr + 1). If they're not equal, set allEqual to false. You don't have to check any more because you know all the elements can't be equal if one pair is not. I won't give the code because at this point you should be able to code the loop yourself.