删除数组中的某几个元素

需求描述

  1. 给定结构体如下:
typedef struct {
    int x;
    int y;
} Point;

typedef struct {
    Point point;
    int Times;
} CCPoints;
  1. 现有数组A CCPoints10个,数组B Point3个
  2. 需要将A中包含B中点的去除

解决思路1:

主要逻辑代码如下:

  1. 第一个for遍历B数组所有元素
  2. 第二个for遍历A数组,如果不满足相等条件,那么覆盖,如果满足则计数
  3. 最后的数组有效元素为原数组长度-满足的计数。
for (int ps = 0; ps < 3; ps++) {
        ps2 = 0;
        for (int cs = 0; cs < len; cs++) {
            if (cc[cs].point.x != tt[ps].x || cc[cs].point.y != tt[ps].y) {
                cc[ps2] = cc[cs];
                ps2++;
            }else{
                cnts++;
            }
        }
    }

完整代码如下:

#include <iostream>
#include <memory>
#include <string.h>

using namespace std;
typedef struct {
    int x;
    int y;
} Point;

typedef struct {
    Point point;
    int Times;
} CCPoints;

int main() {
    int n;
    CCPoints aa[10];
    for (int i = 0; i < 10; i++) {
        aa[i].point = {i, i};
        aa[i].Times = i + 2;
    }
    for (int i = 0; i < 10; i++) {
        cout << "i = " << i << "   " << aa[i].point.x << "  " << aa[i].point.y
             << "  " << aa[i].Times << endl;
    }
    cout << endl;
    const CCPoints* bb = aa;

    Point tt[3];
    tt[0] = {2, 2};
    tt[1] = {4, 4};
    tt[2] = {7, 7};
    auto* cc = new CCPoints[10];
    
    for(int i = 0 ;i<10;i++){
        cc[i].point = {bb[i].point.x,bb[i].point.y};
        cc[i].Times = bb[i].Times;
    }

    int ps2 = 0, cnts = 0;
    int j = 0;
    int len = 10;
    for (int ps = 0; ps < 3; ps++) {
        ps2 = 0;
        for (int cs = 0; cs < len; cs++) {
            if (cc[cs].point.x != tt[ps].x || cc[cs].point.y != tt[ps].y) {
                cc[ps2] = cc[cs];
                ps2++;
            }else{
                cnts++;
            }
        }
    }
    cout << "  cnts  " << cnts << endl << endl << endl;
    
    for (int i = 0; i < len-3; i++) {
        cout << " i " << cc[i].point.x << "  " << cc[i].point.y << "  "
             << cc[i].Times << endl;
    }
    delete[] cc;
}

运行结果

i = 0   0  0  2
i = 1   1  1  3
i = 2   2  2  4
i = 3   3  3  5
i = 4   4  4  6
i = 5   5  5  7
i = 6   6  6  8
i = 7   7  7  9
i = 8   8  8  10
i = 9   9  9  11

  cnts  3


 i 0  0  2
 i 1  1  3
 i 3  3  5
 i 5  5  7
 i 6  6  8
 i 8  8  10
 i 9  9  11

解决思路2

主要逻辑如下:

  1. 第一for遍历A 数组所有元素
  2. 第二for遍历B数组所有元素,如果B中所有元素均与当前遍历的A元素一致则停止该for
  3. 2步骤中:如果B中所有元素均与当前遍历的A元素不一致,那么缓存该值。
  4. 最终的值就是该缓存的值
for (int ps = 0; ps < len; ps++) {
        flag = false;
        for (int cs = 0; cs < 3; cs++) {
            if (bb[ps].point.x == tt[cs].x && bb[ps].point.y == tt[cs].y) {
                flag = true;
                break;
            }
        }
        if (!flag) {
            cc[ps2] = bb[ps];
            ps2++;
        }
    }

完整代码如下:

#include <string.h>

#include <iostream>
#include <memory>

using namespace std;
typedef struct {
    int x;
    int y;
} Point;

typedef struct {
    Point point;
    int Times;
} CCPoints;

int main() {
    int n;
    CCPoints aa[10];
    for (int i = 0; i < 10; i++) {
        aa[i].point = {i, i};
        aa[i].Times = i + 2;
    }
    for (int i = 0; i < 10; i++) {
        cout << "i = " << i << "   " << aa[i].point.x << "  " << aa[i].point.y
             << "  " << aa[i].Times << endl;
    }
    cout << endl;
    const CCPoints* bb = aa;

    Point tt[3];
    tt[0] = {2, 2};
    tt[1] = {4, 4};
    tt[2] = {7, 7};
    auto* cc = new CCPoints[12];

    int ps2 = 0;
    int len = 10;
    bool flag = false;
    for (int ps = 0; ps < len; ps++) {
        flag = false;
        for (int cs = 0; cs < 3; cs++) {
            if (bb[ps].point.x == tt[cs].x && bb[ps].point.y == tt[cs].y) {
                flag = true;
                break;
            }
        }
        if (!flag) {
            cc[ps2] = bb[ps];
            ps2++;
        }
    }
    cout << "  " << ps2 << endl << endl << endl;
    for (int i = 0; i < ps2; i++) {
        cout << " i " << cc[i].point.x << "  " << cc[i].point.y << "  "
             << cc[i].Times << endl;
    }
    delete[] cc;
}

参考:删除数组中的指定元素——C++实现

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

-西门吹雪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值