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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:01:30+00:00 2026-06-10T13:01:30+00:00

Sorry for two questions in succession. I would like to declare a vector of

  • 0

Sorry for two questions in succession.

I would like to declare a vector of an Object as instance data then initialize its size in a constructor, as such:

  class Test
    {
    private:
        std::vector<Object_I_madeup> myVector;
        int n;
    public:
         Test(int n):
           n(n)
        {
            myVector(n); //Intending to set the size of the vector to n entries
        }

I tried to find a .setSize() a la Java, and doubt I should use resize(). I know this isa simple question – but what is the best way to do this?

Thanks

  • 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-10T13:01:31+00:00Added an answer on June 10, 2026 at 1:01 pm

    Add it to your initializers:

    Test (int n):
        myVector(n), n(n) {}
    

    Take note that it should be placed before n because the members get initialized in the order they appear in your class. As well, if all your n member does is keep track of the size of the vector, you might as well just use myVector.size() instead. You should also be a bit more rigorous with invalid values, as here, someone could pass -5 or something of the sort and you wouldn’t stop them. I realize this is a test, but realize an unsigned parameter would be better. As Ed points out, using size_t is your best option because that’s the equivalent of what the vector uses for its size (std::vector<T>::size_type).

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

Sidebar

Related Questions

I have two questions. How to create a NSFileHandle object with the FILE* instance?
I am sorry for the long text but I would like to present the
sorry to combine two questions into one but I'm having trouble finding and answer
I'm sure there have been many questions like these in the past, so sorry
Sorry to combine two questions into one, they are related. HashCode s for HashSet
Sorry for my foolish and I want to ask for two questions, but there
I'm setting up a system with WMDRM and would like to have two servers
Sorry for the ridiculously newbie question, but I'm building a two-tabbed app. In the
it's a bit cheating, because there is actually two questions in the title. on
Two questions: i want to generate a View in my PostGIS-DB. How do i

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.