0
vector <int> col(0);
vector<vector<int> > row(0);

for(i=0;i<10;i++)
col.push_back(some integer);

row.push_back(col);
col.clear();

有人可以告诉我这里有什么问题吗?在col[]向量中,没有错误,但是当我在最后一行之前通过在线调试转到下一条指令时,只是大小row[]从 0 更改为 1,但我看不到它应该从col[]向量中获取的值?

编辑:我试图放置调试屏幕的屏幕截图,但发生了声誉问题。

4

1 回答 1

0

您的代码绝对没有问题:

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector< int > col(0);
    vector< vector<int> > row(0);

    for(int i=0;i<10;i++)
        col.push_back(i);

    row.push_back(col);
    col.clear();

    std::cout << "row size is: " << row.size() << std::endl;
    std::cout << "row 1st elem size is: " << row[0].size() << std::endl;
    std::cout << "col size is: " << col.size() << std::endl;

    return 0;
}

这输出:

row size is: 1
row 1st elem size is: 10
col size is: 0

这是完全可以预料的。可能是您的调试器中的一个错误(它可能不会向您显示实际的向量大小)。

于 2014-10-28T16:21:57.930 回答