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

  • Home
  • SEARCH
  • 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 5953643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:51:20+00:00 2026-05-22T17:51:20+00:00

suppose I have the following class: class MyInteger { private: int n_; public: MyInteger(int

  • 0

suppose I have the following class:

class MyInteger {
private:
  int n_;
public:
  MyInteger(int n) : n_(n) {};
  // MORE STUFF
};

And suppose this class don’t have a default trivial constructor MyInteger(). I must always supply an int to initialize it for some reason. And then suppose that somewhere in my code I need a vector<MyInteger>. How do I initialize each MyInteger component in this vector<>?

I have two situations (probably the solution is the same, but I’ll state them anyway), a normal variable inside a function:

int main(){
    vector<MyInteger> foo(10);  //how do I initialize each 
                                //MyInteger field of this vector? 
    doStuff(foo);
}

and as data in a class:

class MyFunClass {
private:
   vector<MyInteger> myVector;

public:
   MyFunClass(int size, int myIntegerValue) : myVector(size) {}; 
   // what do I put here if I need the 
   // initialization to call MyInteger(myIntegerValue) for all 
   // components of myVector?
};

Is it possible to do it just in the initialization list or must I write the initialization by hand in the MyFunClass(int, int) constructor?

This seems so very basic, and yet I somehow missed it inmy book and can’t find in the web.

  • 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-22T17:51:21+00:00Added an answer on May 22, 2026 at 5:51 pm

    There are many ways to get there. Here are some of them (in no particular order of presence).

    Use vector(size_type n, const T& t) constructor. It initializes vector with n copies of t. For example:

    #include <vector>
    
    struct MyInt
    {
        int value;
        MyInt (int value) : value (value) {}
    };
    
    struct MyStuff
    {
        std::vector<MyInt> values;
    
        MyStuff () : values (10, MyInt (20))
        {
        }
    };
    

    Push elements into vector one by one. This might be useful when values should be different. For example:

    #include <vector>
    
    struct MyInt
    {
        int value;
        MyInt (int value) : value (value) {}
    };
    
    struct MyStuff
    {
        std::vector<MyInt> values;
    
        MyStuff () : values ()
        {
            values.reserve (10); // Reserve memory not to allocate it 10 times...
            for (int i = 0; i < 10; ++i)
            {
                values.push_back (MyInt (i));
            }
        }
    };
    

    Another option is constructor initialization list, if C++0x is an option:

    #include <vector>
    
    struct MyInt
    {
        int value;
        MyInt (int value) : value (value) {}
    };
    
    struct MyStuff
    {
        std::vector<MyInt> values;
    
        MyStuff () : values ({ MyInt (1), MyInt (2), MyInt (3) /* ... */})
        {
        }
    };
    

    Of course, there is an option to provide default constructor and/or use something other than std::vector.

    Hope it helps.

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

Sidebar

Related Questions

Suppose I have the following hierarchy: class A { public: A() private: int aa;
Suppose you have following class: class ProcessController { public List<Process> Active { get {
Suppose I have the following class: public class TestBase { public bool runMethod1 {
Suppose i have the following class. public class Location { public Id { get;
Suppose I have the following class: public class FixExpr { Expr<FixExpr> in; } Now
Suppose I have the following class package { import flash.display.Stage; public class CustomObject {
Suppose I have the following class hierarchy: class A { int foo; virtual ~A()
Suppose you have the following object hierarchy: class Vehicle { public: virtual ~Vehicle() {}
Suppose I have the following situation: 9 class Program 10 { 11 public static
Suppose I have the following declaration: class Over1 { protected: class Under1 { };

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.