C++ 是vector::push_back()制作浅拷贝&;如何解决这个问题

C++ 是vector::push_back()制作浅拷贝&;如何解决这个问题,c++,vector,struct,shallow-copy,C++,Vector,Struct,Shallow Copy,在我正在编写的程序中,我有一些类似于这里的代码: #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}

在我正在编写的程序中,我有一些类似于这里的代码:

#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:;
        }
    }
}
#包括
#包括
#包括
使用名称空间std;
结构人
{
字符串名;
int st;
int-sn[50];
};
int main()
{
无符号整数n,ST[10]={25,18,15,12,10,8,6,4,2,1};
矢量主控;
cin>>n;
对于(int i=0;i>m;

for(int j=0;j
push_back
正在使用其复制构造函数复制要插入的对象。如果没有自定义的对象(如在您的结构中),默认情况下仅使用其自己的复制构造函数复制所有字段,等等


对于包含字符串和基本类型的固定大小数组的结构,结果应该相当于深度复制。

在提交代码之前,您需要找到一种方法来测试代码。@Alan:我测试了小案例,但结果和存储的信息都没有错误。这就是我感到困惑的地方:(你说什么“gnu C++0X”?没有“浅”与“深”的真正概念C++中的拷贝。忘记了你曾经听到过这两个术语。在C++中,你只有正确的VS错误拷贝。每个对象定义了如何通过复制构造函数复制它。@康诺德:诚实的回答:我不知道。我在使用一个叫CordFrand的网站,这就是他们所说的编译器。