Home > Back-end >  Can I create a second vector which contains the identical objects as the first vector?
Can I create a second vector which contains the identical objects as the first vector?

Time:11-15

I am trying to convert a performance critical part of a Java code to a C code.

In Java I work with lists containing a small sample of the original list. When I add objects of the first list to the second list actually only a reference to the object is stored, so I do not copy the object. This is what I would like to achieve in C also. Unfortunately I have not found a way to do so as the push_back method seems to create a deep copy instead.

Java code:

class Data {

    Data(int id){
        this.id = id;
        this.name = "Name "   id;
    }
    public int id;
    public String name = "";
    public boolean isFancy = false;
}

public class Main {
    public static void main(String[] args) {

        List<Data> dataList = new ArrayList<>();

        for (int i = 0; i < 10; i  ) {
            Data data = new Data(i);
            dataList.add(data);
        }

        List<Data> dataSublist = new ArrayList<>();
        dataSublist.add(dataList.get(2));
        dataSublist.add(dataList.get(3));
        dataSublist.add(dataList.get(8));
        dataSublist.forEach(data -> data.isFancy = true); // change isFancy in subList to alter the original object

        System.out.println("Data 3 original isFancy = "   dataList.get(3).isFancy); // is true

    }
}

C code:

class Data {
public:
    Data(int id) { id_ = id; };

    int id_ = 0;
    std::string name_ = "";
    bool isFancy_ = false;
};

int main()
{
    std::vector<Data> dataList;

    for (int i = 0; i < 10; i  ) {
        Data data = Data(i);
        dataList.push_back(data);
    }

    std::vector<Data> dataSublist;
    dataSublist.push_back(dataList[2]);
    dataSublist.push_back(dataList[3]);
    dataSublist.push_back(dataList[8]);
    for (int i = 0; i < dataSublist.size(); i  ) {
        dataSublist[i].isFancy_ = true; // change isFancy in subList to alter the original object
    }

    std::cout << "Data 3 original isFancy = " << ((dataList[3].isFancy_) ? "true" : "false"); // is false

}

How to I get the output to "true" here?

CodePudding user response:

The learning curve for C is not the best. I don't think it's a good idea to just jump into it and try to write efficient code without any experience. Anyway, here it is, I hope it helps:

#include <string>
#include <vector>
#include <memory>
#include <iostream>

class Data
{

public:
    Data(int id)
        : id_{id}
    {
    }

    int id_;
    std::string name_ = "";
    bool isFancy_ = false;
};

int main()
{
    std::vector<std::shared_ptr<Data>> dataList{};

    for (int i = 0; i < 10; i  )
    {
        std::shared_ptr<Data> data = std::make_shared<Data>(i); // Equivalent to `new Data(i)` in Java.
        dataList.push_back(data);
    }

    std::vector<std::shared_ptr<Data>> dataSublist{};

    dataSublist.push_back(dataList[2]);
    dataSublist.push_back(dataList[3]);
    dataSublist.push_back(dataList[8]);

    for (auto data : dataSublist)
    {
        // The `->` represents an indirection and has nothig to do with lambdas.
        data->isFancy_ = true;
    }

    std::cout << "Data 3 original isFancy = " << std::boolalpha << dataList[3]->isFancy_ << std::endl;
}

  • Related