如何将临时对象存储在地图中,以访问其创建上下文之外的对象
How do I store the tmp object in a map, to acess the object outside of it's creation context
我对 C++ 比较陌生。我试图分解问题。
所有代码也在这里:https://onlinegdb.com/wAmLAONkF(可以执行)
基本上,我有一个创建临时对象的函数。对象被推送到 std::vector
:
vector<Student> GetStudents(int some_params)
{
vector<Student> students;
//logic where students are returned
Student s1 { "John", 42 };
Student s2 { "Bill", 12 };
students.push_back(s1);
students.push_back(s2);
return students;
}
为了维护对象,它们存储在外部 std::map
。
map
通过指针将每个 Student
存储在 vector<Student*>
中(必须是 vector<Student*>
!):
map<int, vector<Student*>> m {};
{ //start of new context, other class context in another file, map is passed by reference
int some_params = 5;
vector<Student> students = GetStudents(some_params);
为了将它们存储在地图中,我将引用复制到新的 vector
:
vector<Student*> tmpV;
for(auto & student : students)
tmpV.push_back( &student);
//store them in the map
m.emplace(some_params, tmpV);
}//leave context
//data loss - Students are lost
我知道,问题是因为指向对象的指针存储在本地向量中。在我离开上下文后,指针没有指向有效的地方。
但是我该如何解决呢?
由GetStudents()
编辑的vector<Student>
return需要存储在与[=14=相同范围(或更高范围)的变量中] 指的是它的 Student
s,例如:
map<int, vector<Student*>> m;
vector<Student> students; // <-- move it up here!
{ //start of new context
int some_params = 5;
students = GetStudents(some_params);
vector<Student*> tmpV;
for(auto & student : students)
tmpV.push_back( &student);
//store them in the map
m.emplace(some_params, tmpV);
} //leave context
// no data loss - Students are valid!
否则,GetStudents()
将不得不 return 一个 vector<Student*>
,其中每个 Student
都是通过 new
动态创建的,然后通过 new
销毁delete
在 map
完成后使用它们:
vector<Student*> GetStudents(int some_params)
{
vector<Student*> students;
//logic where students are returned
students.push_back(new Student{ "John", 42 });
students.push_back(new Student{ "Bill", 12 });
return students;
}
...
map<int, vector<Student*>> m;
{ //start of new context
int some_params = 5;
vector<Student*> students = GetStudents(some_params);
//store them in the map
m.emplace(some_params, students);
} //leave context
// no data loss - Students are valid!
...
for(auto &entry : m) {
for (auto *student : entry.second) {
delete student;
}
}
理想情况下,您的 map
应该存储 vector<Student>
或 vector<unique_ptr<Student>>
,但您的要求“ 必须是 vector<Student*>
!" 阻止了这种情况。
我对 C++ 比较陌生。我试图分解问题。
所有代码也在这里:https://onlinegdb.com/wAmLAONkF(可以执行)
基本上,我有一个创建临时对象的函数。对象被推送到 std::vector
:
vector<Student> GetStudents(int some_params)
{
vector<Student> students;
//logic where students are returned
Student s1 { "John", 42 };
Student s2 { "Bill", 12 };
students.push_back(s1);
students.push_back(s2);
return students;
}
为了维护对象,它们存储在外部 std::map
。
map
通过指针将每个 Student
存储在 vector<Student*>
中(必须是 vector<Student*>
!):
map<int, vector<Student*>> m {};
{ //start of new context, other class context in another file, map is passed by reference
int some_params = 5;
vector<Student> students = GetStudents(some_params);
为了将它们存储在地图中,我将引用复制到新的 vector
:
vector<Student*> tmpV;
for(auto & student : students)
tmpV.push_back( &student);
//store them in the map
m.emplace(some_params, tmpV);
}//leave context
//data loss - Students are lost
我知道,问题是因为指向对象的指针存储在本地向量中。在我离开上下文后,指针没有指向有效的地方。
但是我该如何解决呢?
由GetStudents()
编辑的vector<Student>
return需要存储在与[=14=相同范围(或更高范围)的变量中] 指的是它的 Student
s,例如:
map<int, vector<Student*>> m;
vector<Student> students; // <-- move it up here!
{ //start of new context
int some_params = 5;
students = GetStudents(some_params);
vector<Student*> tmpV;
for(auto & student : students)
tmpV.push_back( &student);
//store them in the map
m.emplace(some_params, tmpV);
} //leave context
// no data loss - Students are valid!
否则,GetStudents()
将不得不 return 一个 vector<Student*>
,其中每个 Student
都是通过 new
动态创建的,然后通过 new
销毁delete
在 map
完成后使用它们:
vector<Student*> GetStudents(int some_params)
{
vector<Student*> students;
//logic where students are returned
students.push_back(new Student{ "John", 42 });
students.push_back(new Student{ "Bill", 12 });
return students;
}
...
map<int, vector<Student*>> m;
{ //start of new context
int some_params = 5;
vector<Student*> students = GetStudents(some_params);
//store them in the map
m.emplace(some_params, students);
} //leave context
// no data loss - Students are valid!
...
for(auto &entry : m) {
for (auto *student : entry.second) {
delete student;
}
}
理想情况下,您的 map
应该存储 vector<Student>
或 vector<unique_ptr<Student>>
,但您的要求“ 必须是 vector<Student*>
!" 阻止了这种情况。