鱼C论坛

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

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

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

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

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

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

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
解释一下这是怎么回事?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-5-17 21:05:01 | 显示全部楼层
好长不看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-21 11:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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