I have an object like this:
class Node {
float x, y, z;
size_t tag;
bool isFree;
std::vector<size_t> connections; // Usually ~10-100 in length
};
Just to give you an idea of size. There is list of these node objects containing millions of instances, which I'll call std::vector<Node> masterNodes
. I have a function elsewhere that returns a container of these objects, such as this one:
std::vector<Node> find_nodes()
{
std::vector<Node> nodes;
// copy some elements from masterNodes that meet our conditions
return nodes;
}
My question is would it be more efficient to return a vector of Node* instead, or is my compiler going to optimize this enough that the gain would be minimal for objects like mine? E.g.
std::vector<Node*> find_nodes()
{
std::vector<Node*> nodes;
// point to some elements from masterNodes that meet our conditions
return nodes;
}
I've seen some replies (such as this one) that suggest copies might be nearly as efficient as returning a pointer, acknowledging the danger of returning pointers to vector elements.