vector::push_back() 是否进行浅拷贝&如何解决这个问题

发布于 2024-11-24 13:24:39 字数 1619 浏览 0 评论 0原文

在我正在编写的程序中,我有与此处的代码类似的内容:

#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:;
        }
    }
}

如您所见,我将一个结构体 (people youngling) 推回到一个向量 (vectormaster )。但是,这段代码给了我错误的结果,我认为这可能是由结构和浅复制问题引起的。这在一定程度上得到了证明,因为如果我使用完整的 people 数组来存储输入,那么答案就是正确的。但我对此感到困惑:

  1. struct 只是一个指向编译器的指针吗?或者为什么会存在这个浅复制问题?
  2. 我在内循环中声明了people youngling,希望能解决这个问题,但没有用。有什么简单的方法可以纠正上面的代码片段吗?
  3. 当我使用 GCC 4.4 对小案例进行测试时,答案似乎是正确的。但是,当我使用 gnu C++0X 测试它时,答案是错误的。这是编译器特定的问题吗?

注意:我不能提供错误的测试用例,因为我正在使用判断系统在线测试它。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谷夏 2024-12-01 13:24:39

push_back 正在使用其复制构造函数复制要插入的对象。如果没有自定义字段(例如您的结构的情况),默认情况下只是复制所有字段,使用它们自己的复制构造函数等。

对于您的结构 - 包含一个字符串和一个基本类型的固定大小数组 - 结果应该相当于深拷贝。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文