【问题标题】:pushing back data into 2d vector in c++在c ++中将数据推回二维向量
【发布时间】:2014-12-24 03:32:42
【问题描述】:
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[] 向量?

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

【问题讨论】:

  • 你能剪切和粘贴调试文本吗?
  • 虽然效率低下,但我看不出你贴的代码有什么问题。
  • @GWW 如果你的意思是看屏幕,我不能。
  • col[] 向量到底是什么?

标签: c++ vector push-back 2d-vector


【解决方案1】:

你的代码肯定没有问题:

#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

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

【讨论】:

  • 感谢您的帮助。我想你是对的。我使用的是 DevCpp,现在我使用 CodeBocks 创建了新项目,它运行良好。再次感谢。
  • @asker Bloodshed Dev-C++ 是古老的。 2014 年谁还在推荐这款 Windows 95 软件?
  • @bames53 学校的 IT 成员 :P 所有电脑都安装了它 :)
猜你喜欢
  • 1970-01-01
  • 2021-08-16
  • 1970-01-01
  • 1970-01-01
  • 2017-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多