在 C++ 中将元素添加到向量对中
本文将介绍几种在 C++ 中将元素添加到向量对中的方法。
使用 push_back
和 make_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);
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn