迹忆客 专注技术分享

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

在 C++ 中将向量 vector 追加到另外一个向量 vector

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

本文将介绍几种在 C++ 中如何将一个向量 vector 追加到另一个向量 vector 的方法。

在 C++ 中使用 insert 函数将向量追加到向量上

insert 方法是 std::vector 容器的一个内置函数,它可以向 vector 对象添加多个元素。作为第一个例子,我们展示了如何将一个给定的范围从一个 vector 追加到另一个 vector。如果我们指定三个迭代器作为参数,insert 函数将在作为第一个参数传递的迭代器之前添加最后两个参数的范围中的元素。由于我们在下面的代码示例中传递了第一个 vector 对象的 end 迭代器,因此该函数本质上是在做两个向量的追加。

#include <iostream>
#include <vector>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector;

template<typename T>
void printVectorElements(vector<T> &vec)
{
    for (auto i = 0; i < vec.size(); ++i) {
        cout << vec.at(i) << "; ";
    }
    cout << endl;
}

int main() {
    vector<int> i_vec1 = {12, 32, 43, 53, 23, 65, 84};
    vector<int> i_vec2 = {121, 321, 431, 531, 231, 651, 841};

    cout << "i_vec1           : ";
    printVectorElements(i_vec1);
    i_vec1.insert(i_vec1.end(), i_vec2.begin(), i_vec2.end());
    cout << "i_vec1 (inserted): ";
    printVectorElements(i_vec1);
    cout << endl;

    return EXIT_SUCCESS;
}

输出:

i_vec1           : 12; 32; 43; 53; 23; 65; 84;
i_vec1 (inserted): 12; 32; 43; 53; 23; 65; 84; 121; 321; 431; 531; 231; 651; 841;

或者,我们可以使用 std::end/std::begin 方法指定迭代器,实现更通用的向 insert 函数传递参数的方式。

#include <iostream>
#include <vector>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector;

template<typename T>
void printVectorElements(vector<T> &vec)
{
    for (auto i = 0; i < vec.size(); ++i) {
        cout << vec.at(i) << "; ";
    }
    cout << endl;
}

int main() {
    vector<int> i_vec1 = {12, 32, 43, 53, 23, 65, 84};
    vector<int> i_vec2 = {121, 321, 431, 531, 231, 651, 841};

    cout << "i_vec1           : ";
    printVectorElements(i_vec1);
    i_vec1.insert(std::end(i_vec1), std::begin(i_vec2), std::end(i_vec2));
    cout << "i_vec1 (inserted): ";
    printVectorElements(i_vec1);
    cout << endl;

    return EXIT_SUCCESS;
}

在 C++ 中使用 insert 函数在向量中添加元素

使用 insert 方法的另一种常见方式是向向量添加一系列具有给定值的元素。例如,我们可以在整数向量的前 4 个位置插入 0。注意,第一个参数是元素的位置,在这个位置上添加元素。即使使用 new 调用手动分配 vector 元素,也可以使用 insert 方法。

#include <iostream>
#include <vector>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector;

template<typename T>
void printVectorElements(vector<T> &vec)
{
    for (auto i = 0; i < vec.size(); ++i) {
        cout << vec.at(i) << "; ";
    }
    cout << endl;
}

int main() {
    vector<int> i_vec1 = {12, 32, 43, 53, 23, 65, 84};

    cout << "i_vec1           : ";
    printVectorElements(i_vec1);
    i_vec1.insert(i_vec1.begin(), 4, 0);
    cout << "i_vec1 (inserted): ";
    printVectorElements(i_vec1);
    cout << endl;

    return EXIT_SUCCESS;
}

输出:

i_vec1           : 12; 32; 43; 53; 23; 65; 84;
i_vec1 (inserted): 0; 0; 0; 0; 12; 32; 43; 53; 23; 65; 84;

当需要连接两个字符串向量时,也可以使用 insert 方法。下面的例子演示了与整数向量几乎相同的语法。

#include <iostream>
#include <vector>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector;

template<typename T>
void printVectorElements(vector<T> &vec)
{
    for (auto i = 0; i < vec.size(); ++i) {
        cout << vec.at(i) << "; ";
    }
    cout << endl;
}

int main() {
    vector<string> str_vec1 = {"doordash", "dribble",
                              "renode", "xilinx"};
    vector<string> str_vec2 = {"airbus", "sikorsky"};

    str_vec1.insert(str_vec1.end(), str_vec2.begin(), str_vec2.end());
    printVectorElements(str_vec1);
    cout << endl;

    return EXIT_SUCCESS;
}

输出:

doordash; dribble; renode; xilinx; airbus; sikorsky;

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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便