如何在 C++ 中初始化 const int 二维向量
如何初始化 const int 二维向量:
Const int vector < vector < int > > v ?
v = {1 , 1 ; 1, 0} ?
它不起作用。
How to initialize a const int 2-dimension vector:
Const int vector < vector < int > > v ?
v = {1 , 1 ; 1, 0} ?
it does not work .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以执行此操作(仅在 C++11 中):
另请注意,您不需要编写
>> >
。由于此问题已在 C++11 中修复,因此>>
可以正常工作。它不会被解释为右移运算符(如 C++03 的情况)。You can do this (only in C++11):
Also note that you don't need to write
> >
. As this issue has been fixed in C++11,>>
would work. It wouldn't be interpreted as right-shift operator (as was the case with C++03).如果您的编译器支持 C++11 的初始化列表功能(这就是所谓的吗?),您可以执行以下操作:
请注意,您将无法将任何元素添加到第一个维度(但您可以添加元素)到第二个),例如,
如果您希望第二个维度不可修改,则可以执行
“Then”
,或者如果您希望元素本身为
const
,则可以执行“Then”
,您可能想要修改它在运行时,所以最好删除总共
const
。If your compiler supports the initialisation-list feature (is that what it's called?) of C++11, you can do this:
Note that you won't be able to add any elements to the first dimension (but you can add elements to the second), e.g.
If you wanted the second dimension to be non-modifiable, you'd do
Then
OR if you want the elements themselves to be
const
, you would doThen
You probably want to modify it at runtime, so it's probably good to remove the
const
altogether.将 10x10 数组初始化为 1
Initialises a 10x10 array to 1