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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:26:10+00:00 2026-05-23T19:26:10+00:00

In the program I am writing, I have something similar to the code here:

  • 0

In the program I am writing, I have something similar to the code here:

#include<iostream>
#include<vector>
#include<cstring>

using namespace std;

struct people
{
    string name;
    int st;
    int sn[50];
};

int main()
{
    unsigned int n,ST[10]={25,18,15,12,10,8,6,4,2,1};
    vector<people> master;
    cin>>n;
    for (int i=0;i<n;i++)
    {
        unsigned int m;
        cin>>m;
        for (int j=0;j<m;j++)
        {
            people youngling; //I am declaring it here, but it doesn't solve the issue
            string s;
            cin>>s;
            for (int l=0;l<master.size();l++)
            {
                if (master[l].name.compare(s)==0)
                {
                    if (j<10) master[l].st+=ST[j];
                    master[l].sn[j]++;
                    goto loop;
                }
            }
            youngling.name=s;
            if (j<10) youngling.st=ST[j];
            for (int l=0;l<50;l++) youngling.sn[l]=0;
            youngling.sn[j]++;
            master.push_back(youngling);
            loop:;
        }
    }
}

As you can see, I am pushing back a struct (people youngling) into a vector (vector<people> master). However, this code is giving me the wrong results, and I think it might be caused by the struct and the shallow copy issue. This is proved somewhat since if I use a full array of people to store the input then the answer is correct. But I am puzzled by this:

  1. Is struct just a pointer to the compiler, or why does this shallow copy issue exist?
  2. I am declaring the people youngling inside the inner loop, hoping to solve this issue, but no use. Is there any easy way to correct the code snippet above?
  3. When I am testing on small cases using GCC 4.4, the answer seem to be right. However, when I test it use gnu C++0X, the answer is wrong. Is this a compiler-specific issue?

Note: I cannot provide the wrong test case since I am testing it online using a judge system.

  • 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-23T19:26:11+00:00Added an answer on May 23, 2026 at 7:26 pm

    push_back is making the copy of object being inserted using its copy constructor. If there is no custom one (like in case of your struct), the default is just to copy all fields, using their own copy constructors, etc.

    For your struct – containing a string and a fixed-size array of fundamental type – result should be equivalent to deep copy.

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

Sidebar

Related Questions

I am writing a drawing program, Whyteboard -- http://code.google.com/p/whyteboard/ I have implemented image rotating
Solution for writing a program for keep secret photos. I have many secret photos
I have Virtual PC 2007. I am writing a C# program that will run
I'm writing a program (for Mac OS X, using Objective-C) and I need to
I have been writing some code that detects add and removal of USB devices,
I have been going crazy on this program im writing. My decks dont seem
I'm writing a program that, using Rijndael, will encrypt and decrypt files/folders using a
I am writing a small program in OpenGL on my Mac. I have a
I understand object oriented programming, and have been writing OO programs for a long
I'm writing server-side programs in PHP for an iPhone app. And I have no

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.