迹忆客 专注技术分享

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

在 C++ 中将元素添加到向量对中

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

本文将介绍几种在 C++ 中将元素添加到向量对中的方法。

使用 push_backmake_pair 向向量对中添加元素

vector 容器可以容纳 std::pair 类型的元素,它是将两个异构对象类型作为一个数据单元来容纳的类模板。它类似于 Python 等不同编程语言中较为普遍的 tuple 数据类型,只是它只能容纳 2 个元素。

用表达式-vector<pair<int, string>> 来声明一个成对的向量,它的初始化方式可以和结构体一样。一旦我们需要将额外的 std::pair 类型的元素推送到 vector 中,就可以使用 push_back 方法。但请注意,它需要一个使用 make_pair 函数构造的元素。

在下面的例子中,我们使用 <int, string> 对,向成对的向量中添加元素的语句是 push_back(make_pair(55, "fivety-five"))

#include <iostream>
#include <vector>

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

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

int main() {
    vector<pair<int, string>> vec1 = {{12, "twelve"},
                                      {32, "thirty-two"},
                                      {43, "forty-three"}};

    cout << "vec1: ";
    printVectorElements(vec1);

    vec1.push_back(make_pair(55, "fifty-five"));

    cout << "vec1: ";
    printVectorElements(vec1);
    cout << endl;

    return EXIT_SUCCESS;
}

输出:

vec1: (12,twelve); (32,thirty-two); (43,forty-three);
vec1: (12,twelve); (32,thirty-two); (43,forty-three); (55,fifty-five);

使用 push_back 和 Pair 类型转换向成对向量中添加元素

作为前一种方法的替代方法,我们可以将文字值转换为一对,然后将表达式插入到 push_back 方法中。虽然,这种方法对于可读性来说不那么清晰,而且对于较大的代码库来说,可以说是容易出错。

#include <iostream>
#include <vector>

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

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

int main() {
    vector<pair<int, string>> vec1 = {{12, "twelve"},
                                      {32, "thirty-two"},
                                      {43, "forty-three"}};

    cout << "vec1: ";
    printVectorElements(vec1);

    vec1.push_back(pair(55, "fifty-five"));

    cout << "vec1: ";
    printVectorElements(vec1);
    cout << endl;

    return EXIT_SUCCESS;
}

输出:

vec1: (12,twelve); (32,thirty-two); (43,forty-three);
vec1: (12,twelve); (32,thirty-two); (43,forty-three); (55,fifty-five);

使用 emplace_back 将元素添加到成对向量中

emplace_back 方法是 vector 容器的一个内置函数,它在对象的最后构造一个新的元素。请注意,为了使 emplace_back 方法有效,元素类型应该有一个 args 的构造函数。由于我们使用该函数来构造 std::pair 元素,因此使用文字值调用它是安全的,如下面的代码示例所示。

#include <iostream>
#include <vector>

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

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

int main() {
    vector<pair<int, string>> vec1 = {{12, "twelve"},
                                      {32, "thirty-two"},
                                      {43, "forty-three"}};

    cout << "vec1: ";
    printVectorElements(vec1);

    vec1.emplace_back(55, "fifty-five");

    cout << "vec1: ";
    printVectorElements(vec1);
    cout << endl;

    return EXIT_SUCCESS;
}

输出:

vec1: (12,twelve); (32,thirty-two); (43,forty-three);
vec1: (12,twelve); (32,thirty-two); (43,forty-three); (55,fifty-five);

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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便