c++ - 如何在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];
}
}
解决方案
要使用另一个向量的子范围创建 a vector
,您可以使用带有两个迭代器的构造函数(链接中的 (5) ):
std::vector<float> x(100);
std::vector<flaot> y(x.begin()+begin,x.begin()+end);
y
将拥有从 indexbegin
到end
(end
不包括)从x
.
但是,我宁愿从一开始就将元素存储在正确的位置,而不是将它们读入vector<float> sig(size);
第一个。或者,您可以考虑将所有输入保存在单个向量中,并将迭代器传递给需要处理完整信号的子范围而不是拆分信号的函数。
推荐阅读
- flutter - Flutter DataTable - 点击整行
- android - Android MediaStore API 垃圾图像
- javascript - 如何使用 JavaScript 从子窗口刷新父窗口表
- javascript - 无法在 vee-validate 组件中重新渲染 html
- r - 使用 rlang 中的 `:=` 使用 lapply 函数输入分配列名
- flutter - 我怎样才能使这样的 DataTable 颤动?
- python - 在 python 中使用带有 .format 的元组
- c# - 如何在 Android 和 iOS 的 Xamarin 表单中实现 Deeplink(通用链接)?
- jquery - 无法发送电子邮件 - Azure MVC 站点在尝试通过控制器将表单发布到 ajax 请求时从 jquery.min.js:2 返回 404
- javascript - 如何在 JavaScript 中比较两个日期和时间?