new to C++
I have a question about how to save vector of ptrs.
Approach 1:
std::vector<TypeB> function(const std::vector<const TypeA*>& vector_a) {
std::vector<const TypeB*> vector_b;
for (const auto a : vector_a) {
TypeB b_value = TypeB(*a);
vector_b.emplace_back(&b_value);
}
return function2(vector_b);
}
Which seems only save one item.
I changed the code to: Approach 2:
std::vector<TypeB> function(const std::vector<const TypeA*>& vector_a) {
std::vector<TypeB> vector_b;
for (const auto& a : vector_a) {
const TypeB value_b = EntityStateStruct(*a);
vector_b.emplace_back(value_b);
}
std::vector<const TypeB*> value_b_ptr;
for (const auto& value_b : vector_b) {
value_b_ptr.emplace_back(&value_b);
}
return function2(value_b_ptr);
}
Which is working.
What I guess is in approach 1, the input const std::vector<const TypeA*>& vector_a
will be destroyed when the function ends?
Need some knowledge about this part, Thank you!
Aucun commentaire:
Enregistrer un commentaire