以下是一个示例代码:

#include <iostream>
#include <vector>

int main() {
    int a;
    std::vector<int> b = {1, 2, 3, 4, 5};
    std::vector<int> c;

    for (int i = 0; i < b.size(); i++) {
        bool found = false;
        for (int j = 0; j < c.size(); j++) {
            if (b[i] == c[j]) {
                found = true;
                break;
            }
        }
        if (!found) {
            a = b[i];
            c.push_back(b[i]);
        }
    }

    std::cout << "a: " << a << std::endl;
    std::cout << "c: ";
    for (int i = 0; i < c.size(); i++) {
        std::cout << c[i] << " ";
    }
    std::cout << std::endl;

    return 0;
}

该代码定义了变量a和两个向量b和c。通过双重循环遍历b和c,如果b中的元素与c中的任何一个元素相等,则设置found为true,表示找到相等元素,结束内部循环。如果found为false,表示b中的元素与c中的任何一个元素都不相等,将该元素赋值给a,并将该元素存入c中。最后,输出变量a的值和向量c的内容

C++变量a向量b向量c从头到尾遍历b若对应元素与c中一个元素相等那么遍历下一个如果不相等赋值给a并将该元素存入c中

原文地址: https://www.cveoy.top/t/topic/ivlZ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录