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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:30:51+00:00 2026-05-28T14:30:51+00:00

I want to initialize a vector like we do in case of an array.

  • 0

I want to initialize a vector like we do in case of an array.

Example

int vv[2] = {12, 43};

But when I do it like this,

vector<int> v(2) = {34, 23};

OR

vector<int> v(2);
v = {0, 9};

it gives an error:

expected primary-expression before ‘{’ token

AND

error: expected ‘,’ or ‘;’ before ‘=’ token

respectively.

  • 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-28T14:30:52+00:00Added an answer on May 28, 2026 at 2:30 pm

    With the new C++ standard (may need special flags to be enabled on your compiler) you can simply do:

    std::vector<int> v { 34,23 };
    // or
    // std::vector<int> v = { 34,23 };
    

    Or even:

    std::vector<int> v(2);
    v = { 34,23 };
    

    On compilers that don’t support this feature (initializer lists) yet you can emulate this with an array:

    int vv[2] = { 12,43 };
    std::vector<int> v(&vv[0], &vv[0]+2);
    

    Or, for the case of assignment to an existing vector:

    int vv[2] = { 12,43 };
    v.assign(&vv[0], &vv[0]+2);
    

    Like James Kanze suggested, it’s more robust to have functions that give you the beginning and end of an array:

    template <typename T, size_t N>
    T* begin(T(&arr)[N]) { return &arr[0]; }
    template <typename T, size_t N>
    T* end(T(&arr)[N]) { return &arr[0]+N; }
    

    And then you can do this without having to repeat the size all over:

    int vv[] = { 12,43 };
    std::vector<int> v(begin(vv), end(vv));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Usually like this: #include <boost/assign/std/vector.hpp> vector<int> v; v += 1,2,3,4,5; Except for a: #include
I wanted to initialize a vector of pairs with something like this std::vector< std::pair<bool,
If I want to initialize a vector inside a class, for example: class A
I want to initialize a struct element, split in declaration and initialization. This is
I want to initialize a static collection within my C# class - something like
I want to initialize an array and then initialize a pointer to that array.
I want to initialize a vector with following data of arr for that i
Is there a way to make a non-resizeable vector/array of non-reassignable but mutable members?
I want to be able to initialize a vector of a size 'SIZE' before
I want to initialize a vector in an initialization list of a constructor. The

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.