| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
有整型数组和容器list(存放整型指针),输入完数组后,将此数组放在list里,然后通过swap函数将数组中的两个不同的元素互换(该操作可执行多次),将变换后的数组放在list里 
代码: 
- //版本一
 
 - #include <iostream>
 
 - #include <vector>
 
 - #include <list>
 
 - using namespace std;
 
  
- list<int*> num;
 
  
- void swap(int* array, int x1, int x2);
 
 - void print(list<int*> num);
 
  
- int main() {
 
 -     int *array = new int[9];//定义数组
 
 -     for(int i = 0; i < 9; i++) {
 
 -         cin >> array[i];
 
 -     }
 
 -     num.push_back(array);//把数组放入队列里
 
 -     print(num);
 
 -     cout << endl;
 
 -     swap(array, 0, 1);//下标为0的数下标为1的数互换
 
 -     num.push_back(array);//把交换后的数组放入队列里
 
 -     print(num);
 
 -     cout << endl;
 
 -     swap(array, 0, 2);//下标为0的数下标为2的数互换
 
 -     num.push_back(array);//把交换后的数组放入队列里
 
 -     print(num);
 
 -     cout << endl;
 
 - }
 
  
- void swap(int* array, int x1, int x2) {
 
 -     int temp;
 
 -     temp = array[x1];
 
 -     array[x1] = array[x2];
 
 -     array[x2] = temp;
 
 - }
 
  
- void print(list<int*> num) {
 
 -     list<int*>::iterator it;
 
 -     for(it = num.begin(); it != num.end(); it++) {
 
 -         for(int i = 0; i < 9; i++) {
 
 -             cout << (*it)[i] << " ";
 
 -         }
 
 -         cout << endl;
 
 -     }
 
 - }
 
  复制代码- //版本二
 
 - #include <iostream>
 
 - #include <vector>
 
 - #include <list>
 
 - using namespace std;
 
  
- list<int*> num;
 
  
- int* swap(int* array, int x1, int x2);
 
 - void print(list<int*> num);
 
  
- int main() {
 
 -     int *array = new int[9];//定义数组
 
 -     for(int i = 0; i < 9; i++) {
 
 -         cin >> array[i];
 
 -     }
 
 -     num.push_back(array);//把数组放入队列里
 
 -     print(num);
 
 -     cout << endl;
 
 -     array = swap(array, 0, 1);//下标为0的数下标为1的数互换
 
 -     num.push_back(array);//把交换后的数组放入队列里
 
 -     print(num);
 
 -     cout << endl;
 
 -     array = swap(array, 0, 2);//下标为0的数下标为2的数互换
 
 -     num.push_back(array);//把交换后的数组放入队列里
 
 -     print(num);
 
 -     cout << endl;
 
 - }
 
  
- int* swap(int* array, int x1, int x2) {
 
 -     int temp;
 
 -     temp = array[x1];
 
 -     array[x1] = array[x2];
 
 -     array[x2] = temp;
 
 -     return array;
 
 - }
 
  
- void print(list<int*> num) {
 
 -     list<int*>::iterator it;
 
 -     for(it = num.begin(); it != num.end(); it++) {
 
 -         for(int i = 0; i < 9; i++) {
 
 -             cout << (*it)[i] << " ";
 
 -         }
 
 -         cout << endl;
 
 -     }
 
 - }
 
  复制代码- //版本三
 
 - #include <iostream>
 
 - #include <vector>
 
 - #include <list>
 
 - using namespace std;
 
  
- list<int*> num;
 
  
- int* swap(int* array, int x1, int x2);
 
 - void print(list<int*> num);
 
  
- int main() {
 
 -     int *array = new int[9];//定义数组
 
 -     for(int i = 0; i < 9; i++) {
 
 -         cin >> array[i];
 
 -     }
 
 -     num.push_back(array);//把数组放入队列里
 
 -     print(num);
 
 -     cout << endl;
 
 -     array = swap(array, 0, 1);//下标为0的数下标为1的数互换
 
 -     num.push_back(array);//把交换后的数组放入队列里
 
 -     print(num);
 
 -     cout << endl;
 
 -     array = swap(array, 0, 2);//下标为0的数下标为2的数互换
 
 -     num.push_back(array);//把交换后的数组放入队列里
 
 -     print(num);
 
 -     cout << endl;
 
 - }
 
  
- int* swap(int* array, int x1, int x2) {
 
 -     int temp;
 
 -     int* swapped = array;
 
 -     temp = swapped[x1];
 
 -     swapped[x1] = swapped[x2];
 
 -     swapped[x2] = temp;
 
 -     return swapped;
 
 - }
 
  
- void print(list<int*> num) {
 
 -     list<int*>::iterator it;
 
 -     for(it = num.begin(); it != num.end(); it++) {
 
 -         for(int i = 0; i < 9; i++) {
 
 -             cout << (*it)[i] << " ";
 
 -         }
 
 -         cout << endl;
 
 -     }
 
 - }
 
  复制代码 
样例: 
输入: 
 
输出: 
- 1 2 3 4 5 6 7 8 9
 
  
- 1 2 3 4 5 6 7 8 9
 
 - 2 1 3 4 5 6 7 8 9
 
  
- 1 2 3 4 5 6 7 8 9
 
 - 2 1 3 4 5 6 7 8 9
 
 - 3 1 2 4 5 6 7 8 9
 
  复制代码 
但实际上一旦数组变了,list中的所有元素也随数组而变化,就是数组是什么样,list中的所有元素就是什么样 
输出(三个版本都一样): 
- 1 2 3 4 5 6 7 8 9
 
  
- 2 1 3 4 5 6 7 8 9
 
 - 2 1 3 4 5 6 7 8 9
 
  
- 3 1 2 4 5 6 7 8 9
 
 - 3 1 2 4 5 6 7 8 9
 
 - 3 1 2 4 5 6 7 8 9
 
  复制代码 
解释一下这是怎么回事? |   
 
 
 
 |