# 如何在 C++ 中从一个向量中提取子向量

## 使用 `{}` 列表初始化符号从向量中提取子向量

``````#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;
``````

``````#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;
``````

``````#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;
``````

## 相关文章 - C++ Vector

• 如何在 C++ 中初始化向量