Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6187671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:05:59+00:00 2026-05-24T02:05:59+00:00

I am trying to split a vector into almost equal parts, and create subvectors

  • 0

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.”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T02:05:59+00:00Added an answer on May 24, 2026 at 2:05 am

    The vector constructor (and all STL constructors that accept pointer ranges, for that matter) are designed to take in a range of STL-style iterators. When using iterators, you specify a range by providing a pointer to the first- and the past-the-end elements, not the first and last elements. If you wanted to create a vector as a copy of the subrange (0, 99) out of another vector, you could write

    vector<some> dest(source.begin(), source.begin() + 100);
    

    Note that this uses vector iterators to specify the slice of the first 100 elements rather than operator[], which has undefined behavior when the provided index is out of bounds. In your case, this undefined behavior is manifesting as a runtime debug error. Iterators don’t have this problem, since they’re specifically designed to be used this way.

    If you want to use raw C++ arrays as input to the vector constructor, you could do it like this:

    vector<some> dest(source, source + 100);
    

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greetings, Any input on a way to divide a std::vector into two equal parts
I'm trying to split a string up into words and punctuation, adding the punctuation
I have a useful macro here: #include <algorithm> #include <vector> #include <string> #include <boost/algorithm/string.hpp>
Im trying to split a string on . and create additional columns with the
I'm trying to split a string into an array of words, however I want
I'm trying to split a Maven WAR project into two modules, so that I
I am trying to split a string into an array of word pairs in
I have this code void split(vector<float> &fvec, string str) { int place = 0;
I am trying to split a large XML file into smaller files using java's
I am trying to split a text into an array using explode, but for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.