C++ 如何复制结构数组?

C++ 如何复制结构数组?,c++,struct,C++,Struct,我被卡住了,不知道如何创建阵列的副本。如何使用struct Person数组的原始内容复制它 #include <iostream> #include <iomanip> #include <cstdlib> #include <ctime> using namespace std; struct Person { string name; int age; }; const int arraySize = 2; Perso

我被卡住了,不知道如何创建阵列的副本。如何使用struct Person数组的原始内容复制它

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;


struct Person {
    string name;
    int age;
};

const int arraySize = 2;
Person arrayM[arraySize];
void createArray(Person personArray[], int SIZE);
void printArray(Person personArray[], int SIZE);
int main()
{
    srand(time(NULL));
    cout << "Hello world!" << endl;
    createArray(arrayM, arraySize);
    printArray(arrayM, arraySize);
    return 0;
}

void createArray(Person personArray[], int SIZE)
{
    for(int i = 0; i < arraySize; i++)
    {
        int age1 = rand() % 50 + 1;
        int age2 = rand() % 25 + 1;
        personArray[i].age = age1;
        personArray[i].age = age2;
    }
}

void printArray(Person personArray[], int SIZE)
{
    for(int i = 0; i < SIZE; i++)
    {
        cout << endl;
        cout << personArray[i].age << " " << personArray[i].age;
    }
}

void copyStruct(Person personArray[], int SIZE)
{
    int copyOfArray[SIZE];
    for(int i = 0; i < SIZE; i++)
    {
       ???
    }
}
#包括
#包括
#包括
#包括
使用名称空间std;
结构人{
字符串名;
智力年龄;
};
常数int arraySize=2;
人arrayM[arraySize];
void createArray(Person personArray[],int SIZE);
void printary(Person personArray[],int SIZE);
int main()
{
srand(时间(空));

cout假设,
int copyOfArray[SIZE]
应该是
Person copyOfArray[SIZE]
a,用

copyOfArray[i] = personArray[i];

或者按照basile的建议使用std::array

假设,
int copyOfArray[SIZE]
应该是
个人copyOfArray[SIZE]
a,用

copyOfArray[i] = personArray[i];

或者像basile建议的那样使用std::array,使用
std
算法,更加惯用

void copyStruct(Person personArray[], int SIZE)
{
    Person copyOfArray[SIZE];
    std::copy(
        personArray,
        personArray + SIZE,
        +copyOfArray // + forces the array-to-pointer decay. 
    );
    // Do something with it
}

然而,如前所述,您更应该使用
std::vector
std::array
,它重载
操作符=

,使用
std
算法

void copyStruct(Person personArray[], int SIZE)
{
    Person copyOfArray[SIZE];
    std::copy(
        personArray,
        personArray + SIZE,
        +copyOfArray // + forces the array-to-pointer decay. 
    );
    // Do something with it
}
但是,如前所述,您应该使用
std::vector
std::array
,这将重载
操作符=
,这应该可以:

如下所示定义“copyStruct”函数:

void copyStruct(Person destOfArray[], Person srcArray[], int SIZE)
{
    for(int i = 0; i < SIZE; i++) 
    {
        destOfArray[i].age = srcArray[i].age; 
        destOfArray[i].name = srcArray[i].name;
    }
}
Person copyOfArray[arraySize];
copyStruct(copyOfArray, arrayM, arraySize);

// Now print the content of 'copyOfArray' using your 'printArray' function
printArray(copyOfArray, arraySize);
这应该起作用:

如下所示定义“copyStruct”函数:

void copyStruct(Person destOfArray[], Person srcArray[], int SIZE)
{
    for(int i = 0; i < SIZE; i++) 
    {
        destOfArray[i].age = srcArray[i].age; 
        destOfArray[i].name = srcArray[i].name;
    }
}
Person copyOfArray[arraySize];
copyStruct(copyOfArray, arrayM, arraySize);

// Now print the content of 'copyOfArray' using your 'printArray' function
printArray(copyOfArray, arraySize);


使用和C++11考虑使用stl容器,如
std::vector
要点:无论您如何:copy“personArray…您的变量“int copyOfArray[]”在退出copyStruct()时不再存在。如果您希望在copyStruct()之外使用“copy”,则必须以不同的方式分配“copy”…这似乎与
personArray[i]无关.age=age1
有点毫无意义,因为你
personArray[i].age=age2
在下一行,因此也使得
age1
本身的计算毫无意义。而且你缺少
#include
。使用和C++11考虑使用stl容器,如
std::vector
重要的一点:无论你如何:复制”personArray…当您退出copyStruct()时,变量“int copyOfArray[]”将不再存在。如果您希望在copyStruct()之外使用“副本”,则必须以不同的方式分配“副本”…不相关,这似乎是
personArray[i]。age=age1
有些毫无意义,因为您
personArray[i].age=age2
在下一行,因此也使得
age1
本身的计算毫无意义。而且您缺少
#include
std::vector
的使用还有一个额外的好处,那就是它消除了当前需要的非标准VLA扩展。
+copyOfArray
是故意的吗?如果是这样的话你可能想解释一下。@ WooCurig我想C++ 14,但实际上它不是。@ MalSalter它是在那里迫使数组指针衰变。我没有C++编译器在尝试它没有,所以比编译错误更好。-谢谢编辑!@昆廷你不需要<代码> +<代码>。它很好,只是代码> CopfOfFrase< /Cube >最后一个参数,向量解决方案就是
std::vector copyOfArray(personArray,personArray+SIZE);
,虽然如果OP将
std::size_t
用于序列长度参数,我会有更温暖的感觉。
std::vector
的使用还有一个额外的好处,即它消除了当前需要的非标准VLA扩展。
+copyOfArray
是故意的吗?如果是这样,您可能希望我想,C++是14,但实际上它不是。@ MsAlter是为了迫使数组指针衰变。我没有手边的C++编译器来尝试它,所以比编译错误更安全。-感谢编辑!@昆廷,你不需要<代码> +/>代码>它只是罚款。向量解决方案就是
std::vector copyOfArray(personArray,personArray+SIZE);
,不过如果OP将
std::SIZE\t
用作序列长度参数,我会有更温暖的感觉。为什么要费心于
copyStruct()
Person::Person(Person const&)
,隐式复制构造函数工作得很好。@MSalters:因为帖子所有者想要复制一个结构数组,而不是单个结构。当然,但是如果您确实有一个工作的复制构造函数,您可以使用
std::copy
来复制数组。只有当隐式复制构造函数由于某种原因不工作时,才需要复制字段。@MSalters好的,现在我知道了理解你想说的。为什么要费心于
copyruct()
Person::Person(Person const&)
,隐式复制构造函数工作得很好。@MSalters:因为帖子所有者想要复制一个结构数组,而不是单个结构。当然,但是如果您确实有一个工作的复制构造函数,您可以使用
std::copy
来复制数组。只有当隐式复制构造函数由于某种原因不工作时,才需要复制字段。@MSalters好的,现在我知道了明白你想说什么。