使用内部灰泥构建器绘制地图

Map with an inner stuct builder

本文关键字:绘制 地图 构建 内部      更新时间:2023-10-16

我有一个内部结构为的映射

struct Messages {
    unsigned int id;
    string user_name;
    string content;
    time_t timestamp;
    Messages(const string& a_user_name, const string& a_content) :
        user_name(a_user_name), content(a_content), id(++last_id)
    {
        timestamp = time(0);
    }
};
map<unsigned int, Messages> Global_messages;

当我尝试以的形式访问元素时

cout << Global_messages[i].id;

这给了我一个错误,没有合适的建设者。

std::map::operator[]要求该值是默认可构造的,而Messages则不是这样。您必须使用其他接口insert/emplace来填充映射,使用atfind来检索值