How to copy vector of struct pointer to another vector of struct pointer?

abc abc 351 Reputation points
2020-12-20T17:16:45.43+00:00

Hi,

How to copy vector of struct pointer to another vector of struct pointer?

Thanks

Developer technologies C++
0 comments No comments
{count} votes

Accepted answer
  1. WayneAKing 4,931 Reputation points
    2020-12-20T22:24:40.85+00:00

    By way of illustrating the differences, study this example.

    Note that when using a shallow copy via assignment of one
    vector to another, changes to structs pointed to in one
    vector will appear in the other vector as well. Both
    vectors have pointers to the same structs.

    By contrast, when using a deep copy which allocates new
    structs and copies the contents from the structs in the
    original vector changes to structs in one vector do
    not affect the contents of the structs in the other vector.

    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    struct S {
        int id;
        string name;
    };
    
    void ListVect(vector<S*>& v)
    {
        for (size_t n = 0; n < v.size(); ++n)
        {
            cout << v[n]->id << "  " << v[n]->name << endl;
        }
    }
    
    int main()
    {
        vector<S*> vsp1;
        vector<S*> vsp2;
        S *s1 = new S{1, "Name1"};
        S *s2 = new S{2, "Name2"};
        vsp1.push_back(s1);
        vsp1.push_back(s2);
    
        cout << "Shallow copy:\n";
        ListVect(vsp1);
        vsp2 = vsp1;
        ListVect(vsp2);
    
        cout << endl;
    
        vsp1[0]->id = 3;
        vsp1[0]->name = "Name3";
        ListVect(vsp1);
        ListVect(vsp2);
    
        cout << "\nDeep copy:\n";
        vsp2.clear();
        for (size_t n = 0; n < vsp1.size(); ++n)
        {
            S *s = new S{ vsp1[n]->id, vsp1[n]->name };
            vsp2.push_back(s);
        }
        ListVect(vsp1);
        ListVect(vsp2);
        cout << endl;
    
        vsp1[0]->id = 4;
        vsp1[0]->name = "Name4";
        ListVect(vsp1);
        ListVect(vsp2);
    
        std::cout << "Press any key and Enter to end.";
        char c;
        cin >> c;
    }
    

    Output:

    Shallow copy:
    1 Name1
    2 Name2
    1 Name1
    2 Name2

    3 Name3
    2 Name2
    3 Name3
    2 Name2

    Deep copy:
    3 Name3
    2 Name2
    3 Name3
    2 Name2

    4 Name4
    2 Name2
    3 Name3
    2 Name2
    Press any key and Enter to end.

    • Wayne
    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. David Lowndes 4,726 Reputation points
    2020-12-20T19:34:10.347+00:00

    Taken literally, the answer to your question is that you just assign the object:

    vector2 = vector1;

    However, your question may be a loaded one since you refer to a "vector of struct pointer", which I presume means you have a vector of pointers to struct?

    If so, the question then needs to specify whether you want to copy each structure or not.

    1 person found this answer helpful.
    0 comments No comments

  2. WayneAKing 4,931 Reputation points
    2020-12-20T21:15:35.12+00:00

    As David noted, you need to be more specific about what you want to
    see in the second vector. Do you want to do a shallow copy - in which
    the second vector will hold the same pointers as the first? Or do you
    want to do a deep copy - in which case the second vector will hold
    pointers to new structs which will have initially the same contents as
    the first vector?

    If you do a shallow copy as in David's example using a simple
    assignment, then the pointers in both vectors will point to
    the exact same structs. If you later alter the contents of one of
    those structs in one of the vectors, the change will be reflected
    in the other vector as well.

    If you do a deep copy then the second vector will have pointers
    to different structs than the first vector, but those new structs
    will be initialized with the contents of the structs in the first
    vector. However, as the pointers in each vector will point to
    different structs, altering a struct pointed to in one vector
    will not affect the contents of the structs pointed to in the
    other vector.

    • Wayne
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.