stl list相关程序问题

在看stl方面的内容,然后在Dash上看到这段代码不是太理解,在代码里标出自己不理解的地方,请各位指点.

struct President
{
    std::string name;
    std::string country;
    int year;
 //构造函数,用初始化列表,为什么会用到std::move(),查了下move是左值转为右值,没必要呀.
    President(std::string p_name, std::string p_country, int p_year)
        : name(std::move(p_name)), country(std::move(p_country)), year(p_year)
    {
        std::cout << "I am being constructed.\n";
    }
    //这个地方两个&&,是啥意思?一个&是引用,两个呢?
    President(President&& other)
        : name(std::move(other.name)), country(std::move(other.country)), year(other.year)
    {
        std::cout << "I am being moved.\n";
    }
    //没见过这种写法,这个是默认的=赋值操作符函数?
    President& operator=(const President& other) = default;
};
 
int main()
{
    std::list<President> elections;
    std::cout << "emplace_back:\n";
    elections.emplace_back("Nelson Mandela", "South Africa", 1994);
 
    std::list<President> reElections;
    std::cout << "\npush_back:\n";
    reElections.push_back(President("Franklin Delano Roosevelt", "the USA", 1936));
 
    std::cout << "\nContents:\n";
    for (President const& president: elections) {
        std::cout << president.name << " was elected president of "
                  << president.country << " in " << president.year << ".\n";
    }
    for (President const& president: reElections) {
        std::cout << president.name << " was re-elected president of "
                  << president.country << " in " << president.year << ".\n";
    }
}
阅读 3k
2 个回答

&&是右值引用,建议补一下C++11的相关知识

第二个&&记得是C++0x 11中的引用折叠问题

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进