当前位置:网站首页>Delete some elements in the array

Delete some elements in the array

2022-07-05 17:49:00 -Ximen blowing snow

The requirements are described as follows

  1. A top-level pointer points to a structure CCPoints Array , It's stored here n Structures
  2. Given another structure Point Array
  3. from 1 Delete array from 2 Elements that meet the conditions in

The code implementation is as follows :

#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;
}

Running results

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

Reference resources : Deletes the specified element from the array ——C++ Realization

原网站

版权声明
本文为[-Ximen blowing snow]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207051723305129.html