如何在 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