将数据推入c ++中的2d向量 [英] pushing back data into 2d vector in c++

查看:181
本文介绍了将数据推入c ++中的2d向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

for(i = 0; i <10; i ++)
col.push_back(某个整数);

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

有人能告诉我这里有什么问题吗?在 col [] 向量中,没有错误,但是当我通过调试在前一行前进到下一条指令时,只是size row [] 从0更改为1,但我无法看到它应该从 col [] 向量? / p>

编辑:我试图把调试屏幕的屏幕截图,但声誉发生了。

解决方案

您的代码肯定没有问题:

  #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()<< 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;
}



此输出:

 行大小为:1 
row第一个elem大小为:10
col大小为: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();

Can someone tell me what is wrong here? In the col[] vector, there is no error, but when I go to the next instruction by debug on line the before last line, just size of row[] changes to 1 from 0, but I can't see the values that it should take from the col[] vector?

Edit: I tried to put debug screen's screen-shot but reputation thing happened.

解决方案

There is definitely no problem with your code:

#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;
}

This outputs:

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

Which is perfectly expected. Could be a bug in your debugger (it may not show you the actual vector sizes).

这篇关于将数据推入c ++中的2d向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆