c++ - 为 std::map 中的 std::array 分配多个值

标签 c++ map c++11 stl

当使用 std::array 时,我可以一次赋值:

std::array<int, 3> a2 = {1, 2, 3}; 

但是当上面的数组组合成一个映射时,我不知道最好的方法:

using namespace std;
map <string, array<int, 3>> myMap;

//I'm doing it like below now...

array<int, 3> tempArray = {1,2,3}; // can I save this line somehow?
myMap[myString] = tempArray;

如果这实际上是正确的方法,请告诉我。谢谢!

最佳答案

虽然使用另一个答案中所示的 insert 效率更高,但您也可以使用

myMap["foo"] = {{1,2,3}};

如果简洁的代码对你来说更重要。

关于c++ - 为 std::map 中的 std::array 分配多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15734611/

相关文章:

c++ - 在 c++ 中加速 map<string,int> .find() 的最快方法。按键按字母顺序排列的位置

c++ - std::async 函数串行运行

c++ - usleep 内部循环花费的时间太长

c++ - 多重继承 : same variable name

c++ - 这段获取最低位位置的代码是如何工作的?

c++ - QPixmap 的 loadFromData 函数使用了一些奇怪的缓存机制?

image - 如何使用渐变贴图为 Canvas 中的图像调整 HTML5 Canvas 的色调。

php渐进式 map 生成

c++ - 是否有 C++11 到 C++03 的转换器?

c++ - C++17 中 std::unordered_map 的推导指南