结构体stra 成员 int x , vector<stra>vB, 假如 vB[i] 和 vB[j] 的x相等, 存入vector<stra>,在存入 vector< vector<stra>>&vvBB,写出C++完整过程
时间: 2024-04-13 16:29:23 浏览: 15
以下是一个完整的C++过程,用于将满足条件的stra结构体存入vvBB:
```cpp
#include <iostream>
#include <vector>
struct stra {
int x;
};
void processV(std::vector<stra>& vB, std::vector<std::vector<stra>>& vvBB) {
std::vector<stra> temp;
for (int i = 0; i < vB.size(); i++) {
for (int j = i + 1; j < vB.size(); j++) {
if (vB[i].x == vB[j].x) {
temp.push_back(vB[i]);
temp.push_back(vB[j]);
}
}
}
vvBB.push_back(temp);
}
int main() {
std::vector<stra> vB = { /* 初始化vB */ };
std::vector<std::vector<stra>> vvBB;
processV(vB, vvBB);
// 输出结果
for (const auto& v : vvBB) {
for (const auto& s : v) {
std::cout << "x: " << s.x << std::endl;
}
}
return 0;
}
```
请注意,在上述代码中,`processV`函数遍历vB中的stra结构体,并检查它们的x值是否相等。如果相等,则将这两个stra结构体存入临时的`std::vector<stra>`容器temp中。最后,temp被存入vvBB中,以满足条件的stra结构体为一组。
请根据实际需求初始化vB并使用上述代码进行测试。