首页 > 解决方案 > 如何在c ++中将数组划分为不同的数组

问题描述

我已经在 C++ 中编写了这段代码来将信号读取为数组,现在我需要将此数组分段为 4 或 5 个不同的数组,以便将前 20 个元素放在 array1 中;20-50 在 array2 中等等.. 怎么做在 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];
    }
}

标签: c++

解决方案


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

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

y将拥有从 indexbeginendend不包括)从x.

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


推荐阅读