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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:02:20+00:00 2026-05-26T18:02:20+00:00

I have some trouble understanding the output from the following code: int main(void) {

  • 0

I have some trouble understanding the output from the following code:

int main(void)
{
    vector<TreeNode> tree;
    for(int i = 0; i < 50; i++)
    {
        TreeNode node;
        tree.push_back(node);
    }

    for(int i = 0; i < 50; i++)
    {
        Image image;
        image.Path = "TestPath";
        image.Votes = 0;

        TreeNode node = tree.at(i);
        node.images.push_back(image);
        node.images.push_back(image);
    }

    TreeNode node = tree.at(0);
    cout << "TEST" << endl;
    cout << node.images.size() << endl;
}

Output:

TEST
0

Why is the output 0 and what should I do to fix it? I would expect the size to be 2 and not 0.
It has probably something to do with how structs work. My two defined structs is as follow:

typedef struct
{
    string Path;
    int Votes;
} Image;

typedef struct
{
    int treeIndex;
    float centroid;
    vector<float*> features;
    vector<Image> images;
} TreeNode;

Note that it is not an option for me to use classes instead of structs.

  • 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-26T18:02:21+00:00Added an answer on May 26, 2026 at 6:02 pm

    vector::at returns a reference to an object.

    Your line

    TreeNode node = tree.at(i);
    

    is creating a copy of the TreeNode in node, so when you add images to it, it’s adding them to a new object that’s not in your vector.

    If you instead use

    TreeNode& node = tree.at(i);
    

    It will add the images correctly.

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

Sidebar

Related Questions

I have some trouble achieving adequate real-time performance from my application and wondering if
I have some trouble understanding the need for std::result_of in C++0x. If I understood
I`m having some trouble in understanding how delegates in C# work. I have many
I'm having trouble understanding some web part communication code, and how to make it
I have real trouble understanding mod_rewrite, and got some help in creating a RewriteRule
I have some trouble understanding this one so here it is. I'm trying to
I'm having some trouble understanding how codeigniters loading works. Well first you have the
I'm having trouble understanding the following bit of code that I was hoping would
I have some trouble understanding parsing XML structures with SAX. Let's say there is
it seems that I have some trouble understanding the semantics of the SUID bit,

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.