在 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