python - Is there a best-practice for looping "all the way through" an array in C++? -
In Python, compare each element of the "array" with the "next element" (with the last element with the first element). ) With the following code:
a = [0, 1, 2, 3] in the range I (-1, 3): if [i] + 1> = A [I + 1]: print [i], 'works' Output:
3 works 0 works 1 works 2 works < / Code> If I want to compare the first element of an array with the second, then the second with the third, etc, and finally the last time , I do this Can be in Python with just a loop. Can I do it in C ++? Can I loop such elements, while living in a loop altogether? To illustrate the following examples, here are some C ++ code which has the same functionality as the above python code.
int a [] = {0, 1, 2, 3}; Std :: cout a [3] & lt; & Lt; Std :: endl; (Int i = 0; i & lt; 2; i ++) for std :: cout & lt; & Lt; A [i] & lt; & Lt; Std :: endl; It has the same output, I mean, for some algorithms, I have to manually duplicate the contents of my for loops for a few steps. For, if I want to see that the first element is equal to the second, then the second third, etc., and finally if the last time is equal, then manually add this phase after or after my Loop --- if (a [0] == a [3]) foo (); Is this what I want to do in C ++? I am new to this and I do not want to be rooted in bad behaviors.
for (int = 3; i
Comments
Post a Comment