C++ 尝试对长度为随机[1-100 000]的向量进行子向量化
我正在尝试将一个变量(1-100000)长度向量拆分为随机部分,以便进一步操作。但是我的代码崩溃了,到目前为止,我的调试没有结果。有人能提供一些见解吗C++ 尝试对长度为随机[1-100 000]的向量进行子向量化,c++,vector,split,C++,Vector,Split,我正在尝试将一个变量(1-100000)长度向量拆分为随机部分,以便进一步操作。但是我的代码崩溃了,到目前为止,我的调试没有结果。有人能提供一些见解吗 #include <math.h> #include <vector> #include <iostream> using namespace std; void subVectorize() { //N ways to pick consecutive integer sequences of lengt
#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);
}
#包括
#包括
#包括
使用名称空间std;
无效子向量化(){
//N种选取长度为M(M
+1是一个偏移量,这意味着它将是你的伪随机数+1,因此它不能是0。你的
开始
可以在结束
之后,两者都可能超出范围。刚刚捕捉到它并应用了修复-我想!仍在试图找出随机性;我想我需要播下种子?如果没有时间图书馆,有什么方法可以做到这一点吗?如果我能帮助你,请接受我的答案,或者更新你的问题,让我知道。另外,如果你发现了一个可能不同的解决方案,那么在这里包括它可以帮助其他人找到解决他们问题的正确方法。我选择了一种不同于次矢量化的方式来处理这个问题,但是你的帖子很有帮助,所以我会接受它。我对耽搁表示歉意!
#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;
}
rand( ) % ( 100000 - 1 ) + 1