鱼C论坛

 找回密码
 立即注册
查看: 2615|回复: 1

关于容器与指针(数组)的问题

[复制链接]
发表于 2021-5-17 17:50:29 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
有整型数组和容器list(存放整型指针),输入完数组后,将此数组放在list里,然后通过swap函数将数组中的两个不同的元素互换(该操作可执行多次),将变换后的数组放在list里
代码:
  1. //版本一
  2. #include <iostream>
  3. #include <vector>
  4. #include <list>
  5. using namespace std;

  6. list<int*> num;

  7. void swap(int* array, int x1, int x2);
  8. void print(list<int*> num);

  9. int main() {
  10.     int *array = new int[9];//定义数组
  11.     for(int i = 0; i < 9; i++) {
  12.         cin >> array[i];
  13.     }
  14.     num.push_back(array);//把数组放入队列里
  15.     print(num);
  16.     cout << endl;
  17.     swap(array, 0, 1);//下标为0的数下标为1的数互换
  18.     num.push_back(array);//把交换后的数组放入队列里
  19.     print(num);
  20.     cout << endl;
  21.     swap(array, 0, 2);//下标为0的数下标为2的数互换
  22.     num.push_back(array);//把交换后的数组放入队列里
  23.     print(num);
  24.     cout << endl;
  25. }

  26. void swap(int* array, int x1, int x2) {
  27.     int temp;
  28.     temp = array[x1];
  29.     array[x1] = array[x2];
  30.     array[x2] = temp;
  31. }

  32. void print(list<int*> num) {
  33.     list<int*>::iterator it;
  34.     for(it = num.begin(); it != num.end(); it++) {
  35.         for(int i = 0; i < 9; i++) {
  36.             cout << (*it)[i] << " ";
  37.         }
  38.         cout << endl;
  39.     }
  40. }
复制代码
  1. //版本二
  2. #include <iostream>
  3. #include <vector>
  4. #include <list>
  5. using namespace std;

  6. list<int*> num;

  7. int* swap(int* array, int x1, int x2);
  8. void print(list<int*> num);

  9. int main() {
  10.     int *array = new int[9];//定义数组
  11.     for(int i = 0; i < 9; i++) {
  12.         cin >> array[i];
  13.     }
  14.     num.push_back(array);//把数组放入队列里
  15.     print(num);
  16.     cout << endl;
  17.     array = swap(array, 0, 1);//下标为0的数下标为1的数互换
  18.     num.push_back(array);//把交换后的数组放入队列里
  19.     print(num);
  20.     cout << endl;
  21.     array = swap(array, 0, 2);//下标为0的数下标为2的数互换
  22.     num.push_back(array);//把交换后的数组放入队列里
  23.     print(num);
  24.     cout << endl;
  25. }

  26. int* swap(int* array, int x1, int x2) {
  27.     int temp;
  28.     temp = array[x1];
  29.     array[x1] = array[x2];
  30.     array[x2] = temp;
  31.     return array;
  32. }

  33. void print(list<int*> num) {
  34.     list<int*>::iterator it;
  35.     for(it = num.begin(); it != num.end(); it++) {
  36.         for(int i = 0; i < 9; i++) {
  37.             cout << (*it)[i] << " ";
  38.         }
  39.         cout << endl;
  40.     }
  41. }
复制代码
  1. //版本三
  2. #include <iostream>
  3. #include <vector>
  4. #include <list>
  5. using namespace std;

  6. list<int*> num;

  7. int* swap(int* array, int x1, int x2);
  8. void print(list<int*> num);

  9. int main() {
  10.     int *array = new int[9];//定义数组
  11.     for(int i = 0; i < 9; i++) {
  12.         cin >> array[i];
  13.     }
  14.     num.push_back(array);//把数组放入队列里
  15.     print(num);
  16.     cout << endl;
  17.     array = swap(array, 0, 1);//下标为0的数下标为1的数互换
  18.     num.push_back(array);//把交换后的数组放入队列里
  19.     print(num);
  20.     cout << endl;
  21.     array = swap(array, 0, 2);//下标为0的数下标为2的数互换
  22.     num.push_back(array);//把交换后的数组放入队列里
  23.     print(num);
  24.     cout << endl;
  25. }

  26. int* swap(int* array, int x1, int x2) {
  27.     int temp;
  28.     int* swapped = array;
  29.     temp = swapped[x1];
  30.     swapped[x1] = swapped[x2];
  31.     swapped[x2] = temp;
  32.     return swapped;
  33. }

  34. void print(list<int*> num) {
  35.     list<int*>::iterator it;
  36.     for(it = num.begin(); it != num.end(); it++) {
  37.         for(int i = 0; i < 9; i++) {
  38.             cout << (*it)[i] << " ";
  39.         }
  40.         cout << endl;
  41.     }
  42. }
复制代码

样例:
输入:
  1. 1 2 3 4 5 6 7 8 9
复制代码

输出:
  1. 1 2 3 4 5 6 7 8 9

  2. 1 2 3 4 5 6 7 8 9
  3. 2 1 3 4 5 6 7 8 9

  4. 1 2 3 4 5 6 7 8 9
  5. 2 1 3 4 5 6 7 8 9
  6. 3 1 2 4 5 6 7 8 9
复制代码

但实际上一旦数组变了,list中的所有元素也随数组而变化,就是数组是什么样,list中的所有元素就是什么样
输出(三个版本都一样):
  1. 1 2 3 4 5 6 7 8 9

  2. 2 1 3 4 5 6 7 8 9
  3. 2 1 3 4 5 6 7 8 9

  4. 3 1 2 4 5 6 7 8 9
  5. 3 1 2 4 5 6 7 8 9
  6. 3 1 2 4 5 6 7 8 9
复制代码

解释一下这是怎么回事?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-5-17 21:05:01 | 显示全部楼层
好长不看
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-29 06:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表