需求描述
- 给定结构体如下:
typedef struct {
int x;
int y;
} Point;
typedef struct {
Point point;
int Times;
} CCPoints;
- 现有数组A
CCPoints
10个,数组BPoint
3个 - 需要将A中包含B中点的去除
解决思路1:
主要逻辑代码如下:
- 第一个
for
遍历B数组所有元素 - 第二个
for
遍历A数组,如果不满足相等条件,那么覆盖,如果满足则计数 - 最后的数组有效元素为原数组长度-满足的计数。
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
主要逻辑如下:
- 第一
for
遍历A 数组所有元素 - 第二
for
遍历B数组所有元素,如果B中所有元素均与当前遍历的A元素一致则停止该for
- 2步骤中:如果B中所有元素均与当前遍历的A元素不一致,那么缓存该值。
- 最终的值就是该缓存的值
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;
}