迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > C++ >

在 C++ 中从数组中删除元素

作者:迹忆客 最近更新:2023/03/15 浏览次数:

本文将介绍几种在 C++ 中如何从数组中删除元素的方法。

从 C++ 中使用 std::to_arraystd::remove 函数从数组中删除元素

数组可以在 C++ 中定义为定长或动态数组,并且它们都需要使用不同的方法来删除元素。在此示例中,我们考虑内置的 C 样式固定数组,因为这些数组通常由数字程序操纵以提高效率。

我们将声明一个 int 数组,并删除元素值 2,该元素值在该数组中出现两次。std::remove 是算法库的一部分,它删除指定范围内给定元素的所有实例。

尽管起初,我们使用 std::to_array 函数将 arr 对象转换为 std::array 容器,以便安全地与 std::remove 方法一起使用。后一种算法返回范围的新末端的迭代器,这意味着所得的数组对象仍包含 10 个元素,我们需要将其复制到新位置。由于原始对象是 C 样式的数组,因此我们为 8 个元素的 int 数组分配了新的动态内存,并使用了 std::memmove 函数从 array 对象中复制了内容。请注意,我们使用 std::distance 函数计算了 8 值。

#include <iostream>
#include <array>
#include <iterator>
#include <cstring>

using std::cout; using std::endl;
using std::array; using std::remove;

int main() {
    int arr[10] = {1, 1, 1, 2, 2, 6, 7, 8, 9, 10};
    int elem_to_remove = 2;

    cout << "| ";
    for (const auto &item : arr) {
        cout << item << " | ";
    }
    cout << endl;

    auto tmp = std::to_array(arr);
    auto len = std::distance(tmp.begin(), (tmp.begin(), tmp.end(), elem_to_remove));
    auto new_arr = new int[len];
    std::memmove(new_arr, tmp.data(), len * sizeof(int));

    cout << "| ";
    for (int i = 0; i < len; ++i) {
        cout << new_arr[i] << " | ";
    }
    cout << endl;

    delete [] new_arr;

    return EXIT_SUCCESS;
}

输出:

| 1 | 1 | 1 | 2 | 2 | 6 | 7 | 8 | 9 | 10 |
| 1 | 1 | 1 | 6 | 7 | 8 | 9 | 10 |

使用 std::erasestd::remove 函数从 C++ 中的数组中删除元素

当给定数组的类型为 std::vector 时,会发生此问题的另一种情况。这次,我们有了动态数组特性,使用内置函数进行元素操作更加灵活。

在下面的示例代码中,我们将使用擦除-删除惯用语并删除范围中给定元素的所有出现。请注意,std::erase 需要两个迭代器来表示要删除的范围。因此,它需要 std::remove 算法的返回值来指定起点。请注意,如果仅调用 std::remove 方法,则 arr2 对象的元素将是 - {1, 1, 1, 6, 7, 8, 9, 10, 9, 10}

#include <iostream>
#include <iterator>
#include <vector>

using std::cout; using std::endl;
using std::vector; using std::remove;

int main() {
    vector<int>arr2 = {1, 1, 1, 2, 2, 6, 7, 8, 9, 10};
    int elem_to_remove = 2;

    cout << "| ";
    for (const auto &item : arr2) {
        cout << item << " | ";
    }
    cout << endl;

    arr2.erase(std::remove(arr2.begin(), arr2.end(), elem_to_remove), arr2.end());

    cout << "| ";
    for (const auto &item : arr2) {
        cout << item << " | ";
    }
    cout << endl;

    return EXIT_SUCCESS;
}

输出:

| 1 | 1 | 1 | 2 | 2 | 6 | 7 | 8 | 9 | 10 |
| 1 | 1 | 1 | 6 | 7 | 8 | 9 | 10 |

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Arduino 中停止循环

发布时间:2024/03/13 浏览次数:444 分类:C++

可以使用 exit(0),无限循环和 Sleep_n0m1 库在 Arduino 中停止循环。

Arduino 复位

发布时间:2024/03/13 浏览次数:315 分类:C++

可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。

Arduino 的字符转换为整型

发布时间:2024/03/13 浏览次数:181 分类:C++

可以使用简单的方法 toInt()函数和 Serial.parseInt()函数将 char 转换为 int。

Arduino 串口打印多个变量

发布时间:2024/03/13 浏览次数:381 分类:C++

可以使用 Serial.print()和 Serial.println()函数在串口监视器上显示变量值。

Arduino if 语句

发布时间:2024/03/13 浏览次数:123 分类:C++

可以使用 if 语句检查 Arduino 中的不同条件。

Arduino ICSP

发布时间:2024/03/13 浏览次数:214 分类:C++

ICSP 引脚用于两个 Arduino 之间的通信以及对 Arduino 引导加载程序进行编程。

使用 C++ 编程 Arduino

发布时间:2024/03/13 浏览次数:127 分类:C++

本教程将讨论使用 Arduino IDE 在 C++ 中对 Arduino 进行编程。

Arduino 中的子程序

发布时间:2024/03/13 浏览次数:168 分类:C++

可以通过在 Arduino 中声明函数来处理子程序。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便