Dan array. It is necessary to shift cyclically the elements of this array 2 positions to the left.
4 answers
Let a be an array for shift, and size_array its size and let the array be integer.
int tmp1, tmp2; tmp1 = a[0]; tmp2 = a[1]; for (int i = 0; i < size_array-2; i++) a[i] = a[i+2]; a[size_array-2] = tmp1; a[size_array-1] = tmp2; if you need to move to some other number of positions, then usually apply a sequential shift. You can also create an array equal to the shift, copy the initial elements (memcpy) and shift the remaining elements (memmove) and copy the additional elements back to the end of the original array from the additional array.,
UPD: there are some very interesting explanations for how to make a shift.
- I use the following function for such purposes: <pre> int IndexNormalizer (int count, int index) {return index> = 0? index - count * (index / count) // +: count + index + count * (- (index (+ 1) / count); // +} </ pre> now you can not be afraid of IndexOutOfRangeException if there are 7 elements, and you enter 10 index, then the function converts it to 4, if -10 - to 5. Now the operation a [n] = b [n + -2] will be feasible in any case. - Specter
- But can the algorithm be explained in words, and I'll try to write a program myself? - nullptr
A = [0, 1, 2, 3, 4, 5, 6]- source array possible shift:B = [2, 3, 4, 5, 6, 0, 1]for this each i-th element of array B assign each normalized element of array A with indexi+сдвигB = [5, 6, 0, 1, 2, 3, 4]and for this, each i-th element of array A assign to each normalized element of array B with indexi+сдвиг- Specter
In C ++, there is a function std::rotate for this:
#include <algorithm> #include <cassert> int main() { int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; std::rotate(arr, arr+8, arr+10); // Перемещаем [arr, arr+8) в конец массива. int expected[10] = {8, 9, 0, 1, 2, 3, 4, 5, 6, 7}; assert(std::equal(arr, arr+10, expected)); // Должно получиться как в expected. } An example implementation of rotate from an article on cppreference.com:
template <class ForwardIt> void rotate(ForwardIt first, ForwardIt n_first, ForwardIt last) { ForwardIt next = n_first; while (first != next) { std::iter_swap(first++, next++); if (next == last) { next = n_first; } else if (first == n_first) { n_first = next; } } } std::iter_swap(a, b) swaps the contents of iterators, i.e. swap(*a, *b) .
In the Boost library, as in std , rotate present:
#include <iostream> #include <vector> #include <boost/range/algorithm.hpp> int main() { std::vector<int> vec = {1, 2, 3, 4, 5}; // перемещаем 1,2 в конец массива boost::range::rotate(vec, vec.begin() + 2); std::cout << "результат: "; boost::range::copy(vec, std::ostream_iterator<int>(std::cout, " ")); std::cout << std::endl; return 0; } Displays: result: 3 4 5 1 2
Official documentation here
Source here
int tmp1 = a[0]; int tmp2 = a[1]; System.arraycopy(a, 2, a, 0, a.length - 2); a[size_array-2] = tmp1; a[size_array-1] = tmp2; - Please try to write more detailed answers. I am sure the author of the question would be grateful for your expert commentary on the code above. - Nicolas Chabanovsky ♦