简体   繁体   English

在2D std :: vector C上每个c ++ 11都不起作用?

[英]c++11 for each on 2D std::vector C not working?

Initialize a 2D vector called matrix. 初始化称为矩阵的2D矢量。

std::vector<std::vector<int>> matrix(512,std::vector<int>(512));

Now I want to resize it: 现在我想调整它的大小:

This works: 这有效:

    matrix.resize(50);
    for(int i = 0; i<matrix.size() ; i++)
        matrix[i].resize(50);

And this does not work: 这不起作用:

    matrix.resize(50);
    for(auto ele : matrix)
        ele.resize(50);

I am using for(auto ele : container) in other places, I should have support for C++11, using Microsoft Visual C++ Compiler 12.0. 我在其他地方使用for(auto ele : container) ,我应该使用Microsoft Visual C ++编译器12.0支持C ++ 11。

You are making copies of the inner vectors in each loop iteration. 您正在每个循环迭代中复制内部向量。 You need to use a reference: 您需要使用参考:

for(auto& ele : matrix)
        ele.resize(50);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM