如何在 C++ 中将数组划分为不同的数组

how to divide an array into different arrays in c++

本文关键字:数组 划分 C++      更新时间:2023-10-16

>我用 c++ 编写了这段代码来将信号读取为数组,现在我需要将这个数组分段为 4 或 5 个不同的数组,以便将前 20 个元素放在 array1 中;阵列 20 中的 50-2 个,依此类推. 如何在 C++ 中执行此操作 我的代码 :

int main()
{
// first we use for loop to insert the signal as arry by the user......
int size;
cout << "this programme read your signal as array first you need to "
"determine the size of the array "
<< endl
<< " Enter desired size of the array";
cin >> size;
vector<float> sig(size);
cout << "the signal  " << endl;
for (int x = 0; x < size; x++)
{
cin >> sig[x];
}
}

要使用另一个向量的子范围创建vector,您可以使用在链接中接受两个迭代器( (5( 的构造函数(:

std::vector<float> x(100);
std::vector<flaot> y(x.begin()+begin,x.begin()+end);

y将具有从索引beginend(不包括end(的元素副本x

但是,我宁愿从一开始就将元素存储在正确的位置,而不是先将它们读取到vector<float> sig(size);中。或者,您可以考虑将所有输入保留在单个向量中,并将迭代器传递给需要使用完整信号的子范围而不是分割信号的函数。