我试图了解 std::move。在我的代码中,我正在从内部包含两个字段的std::list<struct Data>
位置移动一个元素,但我没有得到预期的输出。这是我的代码:struct Data
std::string
#include <iostream>
#include <string>
#include <list>
struct Data {
std::string topic {};
std::string msg {};
Data(const std::string& topic, const std::string& msg) {
this->topic = topic;
this->msg = msg;
}
};
int main() {
std::list<Data> data_list;
data_list.push_back(Data("A", std::string(1000, 'a')));
data_list.push_back(Data("B", std::string(1000, 'b')));
data_list.push_back(Data("C", std::string(1000, 'c')));
data_list.push_back(Data("D", std::string(1000, 'd')));
data_list.push_back(Data("E", std::string(1000, 'e')));
while (!data_list.empty()) {
std::cout << (void*)&data_list.front() << "\n";
Data&& d1 = std::move(data_list.front());
data_list.pop_front();
std::cout << d1.topic << ", " << d1.msg << "\n";
std::cout << (void*)&d1 << "\n\n";
}
std::cout << std::endl;
}