【发布时间】:2012-11-02 12:39:15
【问题描述】:
我是 C++ 新手,我正在尝试使用切片列表在 python 中做一件简单的事情, 但我在 c++ 中找不到简单的方法。
我需要重新排序数组以从给定元素开始,例如: 整数数组[] = {1,2,3,4,5}; 重新排序的数组从元素 3 开始: {3,4,5,1,2}
这是我发现的方法,但似乎有点矫枉过正:
void Graph::reorder(int x, MIntArray ¤tArray)
{
MIntArray reorderedIndices;
int index;
for (unsigned int i=0; i<currentArray.length();i++){if(currentArray[i]==x){index=i;}} // get the index
for (unsigned int i=index; i<currentArray.length();i++){reorderedIndices.append(currentArray[i]);} // zero to index
for (unsigned int i=0; i<index;i++){reorderedIndices.append(currentArray[i]);} // index to last
for (unsigned int i=0; i<currentArray.length();i++){currentArray.set(reorderedIndices[i],i);} // transfer
}
任何帮助将不胜感激!
谢谢
路易斯
【问题讨论】:
-
MINtArray 来自哪个库?该类的细节与开发更好的解决方案有关。你也没有明确提出问题。