迹忆客 专注技术分享

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

在 C++ 中的数组中移动元素

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

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

使用 std::rotate 算法在 C++ 中移动数组中的元素

std::rotate 函数是 C++ 算法库的一部分,可以使用 <algorithm> 头文件导入。这个算法将数组元素旋转到左侧。它需要三个迭代器类型的参数,其中第二个参数指定需要作为新构造的范围的第一个元素。同时,第一个和第三个元素是源范围的起始和结束位置的指定符。

注意,std::rotate 可以利用 rbegin/rend 迭代器将元素移到右边。在下面的例子中,函数在具有 10 个整数的 std::vector 对象上调用,并演示了两个方向的操作。

#include <iostream>
#include <vector>
#include <array>
#include <algorithm>

using std::cout; using std::vector;
using std::array; using std::rotate;
using std::endl;

template<typename T>
void printElements(T &v)
{
    cout << "[ ";
    for (const auto &item : v) {
        cout << item << ", ";
    }
    cout << "\b\b ]" << endl;
}

int main()
{
    vector<int> vec = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    printElements(vec);
    rotate(vec.begin(), vec.begin() + 3, vec.end());

    printElements(vec);
    rotate(vec.rbegin(), vec.rbegin() + 3, vec.rend());

    exit(EXIT_SUCCESS);
}

输出:

[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
[ 4, 5, 6, 7, 8, 9, 10, 1, 2, 3 ]

使用自定义封装函数 std::rotate 在 C++ 中移动数组中的元素

或者,我们可以实现封装函数来封装 std::rotate 算法,并要求用户只传递两个参数-要旋转的数组对象和代表要移动的位置数的整数。我们也可以将传递的整数的符号表示为旋转操作的方向。

在这个自定义函数中,我们任意选择正数表示右旋,负数表示左旋。

需要注意的是,这个 rotateArrayElements 函数模板既可以在固定数组对象上工作,也可以在用 C++ 标准库容器构建的动态数组对象上工作。

#include <iostream>
#include <vector>
#include <array>
#include <algorithm>

using std::cout; using std::vector;
using std::array; using std::rotate;
using std::endl;

template<typename T>
void printElements(T &v)
{
    cout << "[ ";
    for (const auto &item : v) {
        cout << item << ", ";
    }
    cout << "\b\b ]" << endl;
}

template<typename T>
int rotateArrayElements(T &v, int dir)
{
    if (dir > 0) {
        rotate(v.rbegin(), v.rbegin() + dir, v.rend());
        return 0;
    } else if (dir < 0) {
        rotate(v.begin(), v.begin() + abs(dir), v.end());
        return 0;
    } else {
        return 1;
    }
}

int main()
{
    array<int, 10> arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    vector<int> vec = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    rotateArrayElements(arr, 3);
    printElements(arr);

    rotateArrayElements(vec, -3);
    printElements(vec);


    exit(EXIT_SUCCESS);
}

输出:

[ 8, 9, 10, 1, 2, 3, 4, 5, 6, 7 ]
[ 4, 5, 6, 7, 8, 9, 10, 1, 2, 3 ]

使用 std::rotate_copy 算法在 C++ 中移动数组中的元素

std::rotate_copy 算法实现了与 std::rotate 相同的操作,只是前者将旋转后的数组元素复制到另一个用附加函数参数指定的范围。

首先,我们需要声明新的范围,在这种情况下,选择 std::vector 类型,构造函数取源 vector 的大小。

然后我们可以调用 rotate_copy 函数,其参数与我们为 std::rotate 指定的参数相同,第四个迭代器表示目标 vector 的开始。

请注意,下面的例子只演示了数组元素的左旋。

#include <iostream>
#include <vector>
#include <array>
#include <algorithm>

using std::cout; using std::vector;
using std::array; using std::rotate;
using std::endl; using std::rotate_copy;

template<typename T>
void printElements(T &v)
{
    cout << "[ ";
    for (const auto &item : v) {
        cout << item << ", ";
    }
    cout << "\b\b ]" << endl;
}

int main()
{
    vector<int> vec1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    printElements(vec1);
    vector<int> vec2(vec1.size());
    rotate_copy(vec1.begin(), vec1.begin() + 3, vec1.end(), vec2.begin());
    printElements(vec2);

    exit(EXIT_SUCCESS);
}
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
[ 4, 5, 6, 7, 8, 9, 10, 1, 2, 3 ]

转载请发邮件至 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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便