在 C++98 标准中,我们可以使用迭代器和 erase 函数来删除 vector 中的元素。以下是一个使用 C++98 标准实现的例子:

#include <iostream>
#include <vector>

int main() {
    std::vector<int> job_restore_vector;
    job_restore_vector.push_back(1);
    job_restore_vector.push_back(2);
    job_restore_vector.push_back(3);
    job_restore_vector.push_back(4);
    job_restore_vector.push_back(5);

    std::vector<int>::iterator it = job_restore_vector.begin();
    while (it != job_restore_vector.end()) {
        if (*it == 3) {
            it = job_restore_vector.erase(it);
        } else {
            ++it;
        }
    }

    for (it = job_restore_vector.begin(); it != job_restore_vector.end(); ++it) {
        std::cout << *it << ' ';
    }
    std::cout << std::endl;

    return 0;
}

在上面的例子中,我们使用迭代器遍历 job_restore_vector,如果当前元素等于 3,则使用 erase 函数删除该元素,并返回下一个元素的迭代器;如果当前元素不等于 3,则将迭代器自增。最后,我们使用循环打印剩余的元素。

输出结果为:1 2 4 5

C++98 标准下删除 std::vector<int> 元素示例

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

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