4

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.

balki
  • 26,394
  • 30
  • 105
  • 151
zw324
  • 26,764
  • 16
  • 85
  • 118
  • 2
    You need to find a way to test your code before you submit it. – Alan Stokes Jul 16 '11 at 14:00
  • @Alan: I tested on the small cases, but there's no error in both the result and the stored information. That's where I got confused:( – zw324 Jul 16 '11 at 14:03
  • 1
    What do you mean by “gnu C++0X”? – Konrad Rudolph Jul 16 '11 at 14:03
  • 1
    There is no real notion of "shallow" vs "deep" copies in C++. Forget you ever heard those two terms. In C++, you only have correct vs incorrect copies. Each object defines how it is copied by implementing a copy constructor. – jalf Jul 16 '11 at 14:03
  • @Konrad: Honest answer: I don't know. I am using a website called Codeforces, and that's what they call the compiler. – zw324 Jul 16 '11 at 14:05
  • Show code that demonstrates wrong results. The code you show looks fine. – Howard Hinnant Jul 16 '11 at 14:07
  • @Howard: This is the code I tested to be wrong, just stripped away the logic. I am trying to make a clear version of my program for you guy's review:) – zw324 Jul 16 '11 at 14:09
  • @Ziyao: Then they're wrong. Use a better website. Seems you're using some version of GCC in C++0x mode. **If you can't demonstrate the breaking testcase, then we can't help you fix it.** – Lightness Races in Orbit Jul 16 '11 at 14:48
  • Your code looks fine and it should be deep copy for your struct definition. – RoundPi Jul 16 '11 at 14:53
  • @Tomalak: Thanks for the advice:) But if you know who built the site (former ACM/ICPC champion coach, etc.), there really is not a better site out there. Even TopCoder is just different, not really better. I don't mean that they are perfect, but even they are not, they are good enough for me to stick:) Thanks nonetheless! – zw324 Jul 16 '11 at 14:55
  • @Ziyao: I don't really care who built the site. If it says "GNU C++0x" then that indicates that that person has no idea what they are talking about. – Lightness Races in Orbit Jul 16 '11 at 16:22
  • There is no relation with c++0x with the question. Removed tag. – balki Jul 17 '11 at 12:03

1 Answers1

13

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.

Xion
  • 22,400
  • 10
  • 55
  • 79