I am trying to split a vector into "almost equal" parts, and create subvectors for those parts.
I have asked the original question here: How to split a vector into n "almost equal" parts but I found a really good solution, what I am trying to use, from here: Best way to extract a subvector from a vector?
I wrote this function for creating integers for start and end values (round is my function which rounds to specified digits.)
void split( double number, double parts )
{
double loop = number / parts;
for( int i = 0; i < parts; i++ )
{
int start = round( i * loop, 0 );
int end = round( ( i + 1 ) * loop - 1, 0 );
cout << start << " " << end << endl;
}
}
This function so far works OK, but my problem is that when I try to run this:
vector<some> dest( &source[start], &source[end] );
Then it creates one LESS element, then required. I mean for example if I call it with
vector<some> dest( &source[0], &source[99] );
then it will have 99 elements, instead of 100. Why is this happening?
How can I fix it? If I use [end+1], then it terminates at the last vector, with "subscript out of range."