【发布时间】:2012-11-11 17:30:05
【问题描述】:
假设我有以下数据结构:
std::map<size_t, double[2] > trace;
如何使用 operator[] 访问其元素?
基本上我想做这样的事情:
trace[0][0] = 10.0;
trace[0][1] = 11.0;
在编译这些代码行时,我收到以下错误:
/usr/include/c++/4.6/bits/stl_map.h:453:11: error: conversion from ‘int’ to non-scalar type ‘std::map<long unsigned int, double [2]>::mapped_type {aka double [2]}’ requested
cmets?
【问题讨论】:
-
std::map<size_t, double[2] >合法吗?我不认为你可以像那样在 STL 中使用数组。 -
这篇文章可能对你有所帮助stackoverflow.com/questions/2582529/…
-
考虑改用
std::map<int, std::array<double, 2>>(如果你有这种可能性,否则std::map<int, boost::array<double, 2> > -
这是不合法的。 c 样式数组不可复制构造,因此不能用作映射值。你用的是什么编译器? gcc 对
ISO C++ forbids casting to an array type double[2]大喊大叫 -
@ybungalobill:但我认为它们是默认可构造的,这就是它们所需要的。如果你想使用
insert,它们只需要复制或移动构造即可。