C++ 如何通过数组初始化std::map?
我有一个任务要用数组填充空的C++ 如何通过数组初始化std::map?,c++,stl,stdmap,C++,Stl,Stdmap,我有一个任务要用数组填充空的std::map myMap const char* s[] = { "car", "sun", "surprise", "asteriks", "alpha", "apple" }; 并使用基于范围的和绑定进行打印。结果,应该像太阳,惊喜 我已经用这种方式实现了最后一个(打印) for (const auto& [k, v] : myMa
std::map myMap
const char* s[] = { "car", "sun", "surprise", "asteriks", "alpha", "apple" };
并使用基于范围的
和绑定
进行打印。结果,应该像太阳,惊喜
我已经用这种方式实现了最后一个(打印)
for (const auto& [k, v] : myMap)
{
cout << "k = " << k << endl;
std::set<std::string>::iterator it;
for (it = v.begin(); it != v.end(); ++it)
{
cout << "var = " << *it << endl;
}
}
for(const auto&[k,v]:myMap)
{
这个怎么样
const char* s[] = { "car", "sun", "surprise", "asteriks", "alpha", "apple" };
std::map<char, std::set<std::string>> myMap;
for (auto str : s)
myMap[str[0]].insert(str);
// Result: {
// 'a' -> {"alpha", "apple", "asteriks"},
// 'c' -> {"car"},
// 's' -> {"sun", "surprise"}
// }
const char*s[]={“汽车”、“太阳”、“惊喜”、“星号”、“阿尔法”、“苹果”};
std::map myMap;
用于(自动str:s)
myMap[str[0]].插入(str);
//结果:{
//'a'>{“alpha”,“apple”,“asteriks”},
//'c'>{“car”},
//'s'>{“太阳”,“惊喜”}
// }
这个怎么样
const char* s[] = { "car", "sun", "surprise", "asteriks", "alpha", "apple" };
std::map<char, std::set<std::string>> myMap;
for (auto str : s)
myMap[str[0]].insert(str);
// Result: {
// 'a' -> {"alpha", "apple", "asteriks"},
// 'c' -> {"car"},
// 's' -> {"sun", "surprise"}
// }
const char*s[]={“汽车”、“太阳”、“惊喜”、“星号”、“阿尔法”、“苹果”};
std::map myMap;
用于(自动str:s)
myMap[str[0]].插入(str);
//结果:{
//'a'>{“alpha”,“apple”,“asteriks”},
//'c'>{“car”},
//'s'>{“太阳”,“惊喜”}
// }
映射包含键和映射值,数组应该如何填充到映射ot并不明显:为什么不使用循环范围来打印集合?映射包含键和映射值,数组应该如何填充到映射ot并不明显:为什么不使用循环范围来打印集合?