我想做这样的事情。有没有一个stl算法可以轻松做到这一点?
for each(auto aValue in aVector)
{
aMap[aValue] = 1;
}
如果您有一个对向量,其中对中的第一项将是映射的键,第二项将是与该键关联的值,您可以使用插入迭代器将数据复制到映射:
std::vector<std::pair<std::string, int> > values {
{"Jerry", 1},
{ "Jim", 2},
{ "Bill", 3} };
std::map<std::string, int> mapped_values;
std::copy(values.begin(), values.end(),
std::inserter(mapped_values, mapped_values.begin()));
或者,您可以从向量初始化地图:
std::map<std::string, int> m2((values.begin()), values.end());
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)