我需要创建一个大小为1024的结构体列表。因此,我尝试通过利用移动语义来优化列表的推操作。但是根据我的知识,移动语义只对堆分配的内存有意义。请问有人能建议更好的方法吗?这是我的代码:
#include <iostream>
#include <list>
struct St {
int32_t arr[1024];
};
int main() {
std::list<St> struct_list;
for(int32_t i = 0; i < 1024; ++i) {
St st; // stack allocated and memory gets deallocated after each loop
std::cout << &st << std::endl;
struct_list.emplace_back(std::move(st)); // I feel std::move doesn't make any sense here even if I implement move constructor, still data gets copied into the std::list which is allocated in heap, so not efficient.
std::cout << &struct_list.back() << "\n" << std::endl;
}
return EXIT_SUCCESS;
}