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 7827159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:44:29+00:00 2026-06-02T09:44:29+00:00

int arr[5] = {0,1,2,3,4}; vector<int> vec; normally we do: vector<int> vec(arr, arr + sizeof(arr)

  • 0
int arr[5] = {0,1,2,3,4};
vector<int> vec;

normally we do:

vector<int> vec(arr, arr + sizeof(arr) / sizeof(int));

but how do initialize a vector vec with only first 3 values of arr?
Also how do I initialize it with the middle 3 values?

I have to initialize it right away, no push_back on multiple lines..

  • 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-06-02T09:44:32+00:00Added an answer on June 2, 2026 at 9:44 am

    The constructor form you are invoking is this**:

    template <class Iterator> vector(Iterator start, Iterator end);
    

    So, you may pass in anything that acts like a pair of iterators. Specifically, you may pass in two pointers, as long as they both point into the same array, and as long as the 2nd doesn’t come before the first (if the pointers are equal, they represent an empty range).

    In your example, you pass in a pointer to the first element of the array and a pointer to the (mythical) elment-after-the-last-element of the array:

    vector<int> vec(arr, arr + sizeof(arr) / sizeof(int));
    

    Generally, you may pass in a pointer to any element as the start, and a pointer past any element as the finish.

    but how do I initialize a vector vec with only first 3 values of arr?

    Pass in pointers to the first and one past the third elements:

    vector<int> vec(arr, arr+3);
    

    Also how do I initialize it with middle 3 values?

    Pass in a pointer to the first item you want to copy, and a pointer to one paste the final element. In this case, indexes 1 and 4:

    vector<int> vec(arr+1, arr+4);
    

    ** Okay, it is slightly more complicated than that, but it’s okay to pretend for the sake of this discussion. The actual form is: template <class InputIterator>
    vector( InputIterator first, InputIterator last,
    const Allocator& alloc = Allocator() );

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

Sidebar

Related Questions

Lets say I have an array like int arr[10][10]; Now i want to initialize
I have the following piece of code in C++. int arr[] = {1, 2,
I want to initialize a vector with following data of arr for that i
I have an array int arr[5] that is passed to a function fillarr(int arr[])
If I have the next array: int[] arr = { 123, 243, 0, 0,
I have the following two dimensional array: static int[,] arr = new int[5, 5]
Here is the code I have so far: public static int mode(int[][] arr) {
I have a struct : struct ABC { int size; int arr[15]; }; I
I have an array of int pointers int* arr[MAX]; and I want to store
The below fails to compile: typedef int arr[10]; int main(void) { return sizeof arr;

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.