正在尝试对随机[1-100000]长度的矢量进行子矢量化
Trying to Subvectorize Vector of Random [1-100 000] Length
我正试图将一个可变长度(1-100000)的向量拆分为随机部分以进行进一步的操作。但是我的代码崩溃了,到目前为止,我的调试一直没有结果。有人能提供一些见解吗?
#include <math.h>
#include <vector>
#include <iostream>
using namespace std;
void subVectorize() {
//N ways to pick consecutive integer sequences of length M (M < N) K-Vlaue is Kth smallest number in sequence
//Task: Find smallest possible subsequence
int M = rand() % (100000 - 1);
vector<int> nVector(M); //Vector to store sequence of variable length
for (int i = 0; i < nVector.size(); i++) //Populate the vector
{
nVector[i] = i;
}
//Split the vector randomly
vector<int>::const_iterator start = nVector.begin() + (rand() % (100000 - 1));
vector<int>::const_iterator end = nVector.begin() + (rand() % (100000 - 1));
vector<int> subVector(start, end);
}
#include <cmath>
#include <vector>
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void subVectorize( )
{
//N ways to pick consecutive integer sequences of length M (M < N) K-Vlaue is Kth smallest number in sequence
//Task: Find smallest possible subsequence
int M;
int randomEnd;
int randomStart;
M = rand( ) % ( 100000 - 1 ) + 1;
randomEnd = rand( ) % ( M );
if( randomEnd == 0 )
{
++randomEnd;
}
randomStart = rand() % ( randomEnd );
vector<int> nVector(M); //Vector to store sequence of variable length
for ( unsigned int i = 0 ; i < nVector.size( ) ; i++ ) //Populate the vector
{
nVector[i] = i;
}
//Split the vector randomly
vector<int>::const_iterator start = nVector.begin() + randomStart;
vector<int>::const_iterator end = nVector.begin() + randomEnd;
vector<int> subVector( start , end );
}
int main()
{
srand( time( NULL ) );
subVectorize( );
return 0;
}
如果你这样调整,它应该可以正常工作。
我已经包含了伪随机种子。
基本上,你的问题是start
和end
都可能超出范围,end
可能在start
之前,如果M
是0呢?你不能创建一个大小为0的向量,而且你的代码无论如何都会访问它,这对0向量不起作用。
rand( ) % ( 100000 - 1 ) + 1
+1是一个偏移量,这意味着它将是你的伪随机数+1,因此它不可能是0。
相关文章:
- 为什么 openmp 的并行不适用于矢量化色彩空间转换?
- GCC 4.8.2 自动矢量化由于 cout 而失败
- 为什么浮点数的矢量化比双精度更有效?
- GCC、CLANG 和 MSVC 的可视化C++自动矢量化要求
- 如何使 msvc 矢量化浮点添加?
- 我可以期望某些 STL 函数实现是可自动矢量化的吗?
- 当在循环中使用时,std::shared_ptr 对该循环的矢量化有任何影响吗?
- 矢量化图像处理
- MSVC 2017 是否支持具有自动矢量化的 AVX 512
- 矢量化对称矩阵
- 如何在块复制期间矢量化范围检查
- 是否可以使用G 或Clang -OpenMP获得矢量化报告
- 错误的矢量化代码会影响可伸缩性吗?
- 分割故障:从2D矢量中随机删除元素
- C 矢量化双回路
- 如何将现有的矢量化函数与Intel编译器自动化的现有标量函数相关
- C++ 通过 range for 循环将整数输入到矢量中;随机 0
- 用于自动矢量化的展开指针增量循环
- 这是矢量化的良好实践吗
- 对于使用 C 样式指针矢量化的循环,但不使用迭代器