数组与向量的比较 向量/数组相同元素去重

本文探讨了C++模板化的向量bubbleSort_vec与数组bubbleSort_arr的实现,以及利用set进行向量和数组中重复元素的高效去重方法。通过实例展示了如何在C++和Python中实现冒泡排序算法,并介绍了两种去重技术的使用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数组的冒泡排序与向量vector的冒泡排序

#include <bits/stdc++.h>
using namespace std;
template<typename T>
vector<T> bubbleSort_vec(vector<T> a){
    vector<T> res = a;
    for (int i = 0; i < res.size() - 1; i++){
        for (int j = i + 1; j < res.size(); j++){
            T temp;
            if (res[i] > res[j]){
                temp = res[i];
                res[i] = res[j];
                res[j] = temp;
            }
        }
    }
    return res;
}

template<typename T>
T* bubbleSort_arr(T *a, int n){
    T* res = new double[n];
    for (int i = 0; i < n; i++){
        res[i] = a[i];
    }
    T temp;
    for (int i = 0; i < n - 1; i++){
        for (int j = i + 1; j < n; j++){
            if (res[i] > res[j]){
                temp = res[i];
                res[i] = res[j];
                res[j] = temp;
            }   
        }
    }
    return res;
}
int main(){
    vector<double> In1;
    cout << "Enter the number of test data:" << endl;
    int n;
    cin >> n;
    double *In2 = new double[n];
    cout << "Enter the data:" << endl;
    int temp;
    for (int i = 0; i < n; i++){
        cin >> temp;
        In2[i] = temp;
        In1.push_back(temp);
    }
    cout << "Executing bubble sort(vector version)..." << endl;
    vector<double> res1;
    res1 = bubbleSort_vec(In1);
    cout << "result:" << endl;
    for (int i = 0; i < res1.size(); i++){
        cout << res1[i] << endl;
    }
    cout << "\n\n\n";
    cout << "Executing bubble sort(array version)..." << endl;
    double *res2 = bubbleSort_arr(In2,n);
    cout << "result:" << endl;
    for (int i = 0; i < n; i++){
        cout << *(res2 + i) << endl;
    }
    delete[] In2;
    delete res2;
    cout << "process finished..." << endl;
    system("pause");
    return 0;
}

利用std的set实现向量相同元素去重以及数组简单去重

#include <bits/stdc++.h>
using namespace std;

template<typename T>
vector<T> delRepeat_v1(vector<T> a){
    set<T> temp;
    vector<T> res;
    for (int i = 0; i < a.size(); i++){
        if (!temp.count(a[i])){
            temp.insert(a[i]);
            res.push_back(a[i]);
        }
    }
    return res;
}

template<typename T>
vector<T> delRepeat_v2(vector<T> a){
    vector<T> temp;
    for (int i = 0; i < a.size(); i++){
        int flag = 1;
        for (int j = 0; j < temp.size(); j++){
            if (a[i] == a[j]) {
                flag = 0;
                break;
            }
        }
        if (flag) temp.push_back(a[i]);
    }
    return temp;
}


int main(){
    cout << "enter the number of test data:\n";
    int n;
    cin >> n;
    vector<double> In;
    double temp;
    cout << "enter the data:" << endl;
    for (int i = 0; i < n; i++){
        cin >> temp;
        In.push_back(temp);
    }
    cout << "delete the repeated data(use set to ensure every element is unique)...\n";
    vector<double> res = delRepeat_v1(In);
    cout << "result:" << endl;
    for (int i = 0; i < res.size(); i++){
        cout << res[i] << endl;
    }
    cout << "\n\n\n";
    cout << "delete the repeated data(ordinary version)...\n";
    vector<double> res2 = delRepeat_v2(In);
    cout << "result:" << endl;
    for (int i = 0; i < res.size(); i++){
        cout << res[i] << endl;
    }
    cout << "process finished...\n";
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值