如何在 C++ 中從一個向量中提取子向量
本文將介紹幾種如何從 C++ 向量中提取子向量的方法。
使用 {}
列表初始化符號從向量中提取子向量
提取子向量的一種方法是用原始向量元素初始化一個新的向量。下面的方法是用指向所需位置的迭代器指定元素(來自 int_vec
的前 5 個元素)。注意,在這個例子中,我們使用 std::copy
方法將向量元素輸出到控制檯。
#include <iostream>
#include <vector>
#include <iterator>
#include <iomanip>
using std::cout; using std::endl;
using std::vector; using std::copy;
using std::setw;
int main() {
vector<int> int_vec {1, 23, 43, 324, 10, 222, 424,
649, 1092, 110, 129, 40, 3024};
vector<int> sub_vec {int_vec.begin(), int_vec.begin()+5};
cout << std::left << setw(10) << "vec: ";
copy(int_vec.begin(), int_vec.end(),
std::ostream_iterator<int>(cout,"; "));
cout << endl;
cout << std::left << setw(10) << "subvec: ";
copy(sub_vec.begin(), sub_vec.end(),
std::ostream_iterator<int>(cout,"; "));
cout << endl;
return EXIT_SUCCESS;
}
輸出:
vec: 1; 23; 43; 324; 10; 222; 424; 649; 1092; 110; 129; 40; 3024;
subvec: 1; 23; 43; 324; 10;
或者,你也可以通過指定指向原始向量所需元素的指標(例如&int_vec[index]
)來初始化一個子向量變數。
#include <iostream>
#include <vector>
#include <iterator>
#include <iomanip>
using std::cout; using std::endl;
using std::vector; using std::copy;
using std::setw;
int main() {
vector<int> int_vec {1, 23, 43, 324, 10, 222, 424,
649, 1092, 110, 129, 40, 3024};
vector<int> sub_vec {&int_vec[0], &int_vec[5]};
cout << std::left << setw(10) << "vec: ";
copy(int_vec.begin(), int_vec.end(),
std::ostream_iterator<int>(cout,"; "));
cout << endl;
cout << std::left << setw(10) << "subvec: ";
copy(sub_vec.begin(), sub_vec.end(),
std::ostream_iterator<int>(cout,"; "));
cout << endl;
return EXIT_SUCCESS;
}
輸出:
vec: 1; 23; 43; 324; 10; 222; 424; 649; 1092; 110; 129; 40; 3024;
subvec: 1; 23; 43; 324; 10;
使用 copy()
函式從向量中提取子向量
copy()
函式是操縱基於範圍的資料的有力工具。在本例中,我們將利用它從一個向量中複製特定的元素到另一個向量中(意為一個子向量)。首先,我們宣告一個 sub_vec
變數,其中包含一些要複製的元素。接下來,我們將原來的 vector
範圍作為引數傳遞給 copy()
函式,並將 begin
迭代器傳遞給目標 vector
。
#include <iostream>
#include <vector>
#include <iterator>
#include <iomanip>
using std::cout; using std::endl;
using std::vector; using std::copy;
using std::setw;
constexpr int NUM_ELEMS_TO_COPY = 6;
int main()
{
vector<int> int_vec {1, 23, 43, 324, 10, 222, 424,
649, 1092, 110, 129, 40, 3024};
vector<int> sub_vec (NUM_ELEMS_TO_COPY);
copy(&int_vec[0], &int_vec[NUM_ELEMS_TO_COPY], sub_vec.begin());
cout << std::left << setw(10) << "subvec: ";
copy(sub_vec.begin(), sub_vec.end(),
std::ostream_iterator<int>(cout,"; "));
cout << endl;
return EXIT_SUCCESS;
}
subvec: 1; 23; 43; 324; 10; 222;
另一個使用 copy()
方法的強大技術是將一個子向量附加到任何現有的向量變數上。下面的示例程式碼通過使用 std::back_inserter
函式模板將 5 個提取的元素推回到 sub_vec
變數中來演示。注意,copy
方法對元素範圍的操作為 [first, last)
。
#include <iostream>
#include <vector>
#include <iterator>
#include <iomanip>
using std::cout; using std::endl;
using std::vector; using std::copy;
using std::setw;
int main()
{
vector<int> int_vec {1, 23, 43, 324, 10, 222, 424,
649, 1092, 110, 129, 40, 3024};
vector<int> sub_vec = { 1, 2, 3, 4, 5, 6 };
copy(&int_vec[0], &int_vec[5], std::back_inserter(sub_vec));
cout << std::left << setw(10) << "subvec: ";
copy(sub_vec.begin(), sub_vec.end(),
std::ostream_iterator<int>(cout,"; "));
cout << endl;
return EXIT_SUCCESS;
}
subvec: 1; 2; 3; 4; 5; 6; 1; 23; 43; 324; 10;
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