数组索引,内容交换
- 将数组的索引和他的内容交换一下
- 例如 a[4] = {3,2,0,1},交换完成之后就是 a[4] = {2,3,1,0};
- 代码如下:
void swapArr(vector <int> & a, int currIndex, int count){
int tmp = a[currIndex]; //首先记下来这个位置上本来的值
if (count < a.size()){
swapArr(a, a[currIndex], count + 1);
}
a[tmp] = currIndex;
}
int main()
{
vector<int> a = {3,2,0,1};
int tmp = a[0];
swapArr(a, 0, 0);
for (int i = 0; i < a.size(); ++i){
cout << a[i] << endl;
}
system("pause");
}