首页 > 解决方案 > 如何在 C++ 中移动二维数组的序列?

问题描述

我想实现一个能够将二维数组的序列转换(旋转)为所需目标索引的函数。A、B 和 C 表示序列的长度。源是要旋转的序列的开始。下面示例中的源将是 A。Dst 是目标移动开始的索引。输入/输出示例: 之前:

double A[][2] = { {0,0}, {1,1}, {2,2}, {3,3}, {4,4}, {5,5}, {6,6}, {7,7} };
                          ^dst                          A      B      C

调用 translate(A, 8, 5, 3, 1); 后:

{ {0,0}, {5,5}, {6,6}, {7,7}, {1,1}, {2,2}, {3,3}, {4,4} };
           A      B      C

A、B、C 和 dst 只是函数应该做什么的可视化。

我希望能够将序列(索引、A、B 和 C)旋转到所需的目标索引(dst)。以下是我尝试过的尝试:

/*
A-list of locations; 2d array
cities- number of cities
src-index of the beginning of the sequence to be moved
len- length of sequence to translate
dst-index of the beginning of the target of moving
*/

void translate(double A[][2], int cities, int source, int length, int 
 destination){
            vector<vector<int>> variable;
            //to move sequence
            for(int i = (source + length) % cities; i < destination; i++){
                vector<int> variable2;
                variable2.push_back(A[i][0]);
                variable2.push_back(A[i][1]);
                variable.push_back(variable2);
            }
             
            else{
                return;
             }
             
             //get vector into array
             for(int i = 0; i < citiesl i++){
                 A[i][0] = variable[i][0];
                 A[i][1] = variable[i][1];
              }
        }

我收到的错误是

调试断言失败:向量下标超出范围

该函数编译后不返回任何内容。有没有办法在没有向量的情况下做到这一点?

标签: c++algorithm

解决方案


如果您愿意使用标准库容器,那么有一个标准库函数可以帮助您实现这一目标:std::rotate

std::vector<std::pair<int,int>> A = { {0,0}, {1,1}, {2,2}, {3,3}, {4,4}, {5,5}, {6,6}, {7,7} };

// Rotate left by n
std::rotate(A.begin(), A.begin()+n, A.end());

// Rotate right by n
std::rotate(A.rbegin(), A.rbegin()+n, A.rend());

在你的情况下,这可以写成不包括第一个元素

template <template <typename T, typename Alloc> class Cont, typename T, typename Alloc>
void custom_rotate(Cont<T, Alloc>& vec, std::size_t const source_index, std::size_t const destination_index) {
  // Rotate right excluding first element
  if (source_index < destination_index) {
    std::size_t const digits {destination_index - source_index};
    std::rotate(vec.rbegin() + 1, vec.rbegin() + 1 + digits, vec.rend());
  // Rotate left excluding first element
  } else if (source_index > destination_index) {
    std::size_t const digits {source_index - destination_index};
    std::rotate(vec.begin() + 1, vec.begin() + 1 + digits, vec.end());
  }
  return;
}

在这里试试。


编辑

在与原始海报交谈后,我们意识到他想要的不是轮换,而是翻译。这也可以通过插入和删除向量的元素来完成,如下所示

template <template <typename T, typename Alloc> class Cont, typename T, typename Alloc>
void translate(Cont<T, Alloc>& vec, std::size_t const source_index, std::size_t const destination_index, std::size_t const number_of_elements) {
  // Insert elements
  vec.insert(vec.begin()+destination_index, vec.begin()+source_index, vec.begin()+source_index+number_of_elements);
  // Delete elements
  vec.erase(vec.begin()+source_index+number_of_elements, vec.begin()+source_index+2*number_of_elements);
  return;
}

在这里试试


推荐阅读