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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:21:29+00:00 2026-06-03T16:21:29+00:00

I am actually trying to initialize vectors in constructors of struct/class. I came up

  • 0

I am actually trying to initialize vectors in constructors of struct/class.

I came up with these, the below content compiles without error, however does not print out the contents in the vector. I will like to know why, help will be definitely appreciated!

struct MyInt
{
friend ostream &operator<<(ostream &printout, const MyInt &Qn)
{
    printout<< Qn.value << endl;
    return printout;
}

     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...
    }
};

int main()
{
MyStuff *mystuff1;
MyStuff *mystuff2;

for (int i = 0; i < 10; ++i)
{
        mystuff1->values.push_back (MyInt (i));
}

for (int x = 0; x < 5; ++x)
{
        mystuff2->values.push_back (MyInt (x));
}

vector<MyInt>::iterator VITER;

for (VITER =mystuff1->values.begin(); VITER!=mystuff1->values.end(); ++VITER)
{
    cout<< *VITER;
}

return 0;
}
  • 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-03T16:21:32+00:00Added an answer on June 3, 2026 at 4:21 pm

    Undefined behaviour. Your pointers are invalid. Since you don’t need pointers here at all, just go with values:

    MyStuff mystuff1, mystuff2;
    
    // ...
    mystuff1.push_back(...); // et cetera
    

    Also, if you don’t reserve, it still wouldn’t allocate 10 times. vector implementations are smart enough to not increase capacity by 1 each time.

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

Sidebar

Related Questions

I'm trying to design a class which has two vectors of large sequences. std::vector<double>
EDIT: Changed example below to one that actually demonstrates the SIOF. I am trying
I have been trying to initialize 1 vector and 1 map in a class.
Im actually trying to paint an elipse around my cursor when i moove it,
I am actually trying to start a process for winzip and zip a folder.
What I'm actually trying to do is put a WebKitView into a ScreenSaver (which
I am actually trying to change the color index for the first word with
I'm actually trying to fix this issue where my projects are always out of
Well i'm actually working on a Memory Injector. What i'm actually trying to do
is there similar syntax to php's $$variable in python? what I am actually trying

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.