打印2d矢量的内容 [英] printing contents of 2d vector

查看:203
本文介绍了打印2d矢量的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我正在运行的代码:

this is the code I am running:

std::vector<std::vector<double>> test;
test.push_back(std::vector<double>(30));

 std::vector<std::vector<double> >::iterator it=test.begin(), end=test.end();
    while (it!=end) {
      std::vector<double>::iterator it1=it->first.begin(),end1=it->first.end();
      while (it1!=end1) {
    std::copy(it1.begin(),it1.end(),std::ostream_iterator<double>(std::cout, " "));
    ++it1;
      }
      ++it;
    }

这是我得到的编译错误:

this is the compilation error I get:

data.cpp:33:45: error: ‘class std::vector<double>’ has no member named ‘first’
data.cpp:33:68: error: ‘class std::vector<double>’ has no member named ‘first’
data.cpp:35:16: error: ‘class std::vector<double>::iterator’ has no member named ‘begin’
data.cpp:35:28: error: ‘class std::vector<double>::iterator’ has no member named ‘end’
data.cpp:35:34: error: ‘ostream_iterator’ is not a member of ‘std’
data.cpp:35:56: error: expected primary-expression before ‘double'

有关如何修复它的任何建议,以便我打印test

any suggestions on how to fix it so I can print the contents of test

推荐答案

我认为这是更多你想要的。

I think this is more what you want.

std::vector<std::vector<double>> test;
// Put some actual data into the test vector of vectors
for(int i = 0; i < 5; ++i)
{
    std::vector<double> random_stuff;
    for(int j = 0; j < 1 + i; ++j)
    {
        random_stuff.push_back(static_cast<double>(rand()) / RAND_MAX);
    }
    test.push_back(random_stuff);
}

std::vector<std::vector<double> >::iterator it=test.begin(), end=test.end();
while (it!=end) 
{
    std::vector<double>::iterator it1=it->begin(),end1=it->end();
    std::copy(it1,end1,std::ostream_iterator<double>(std::cout, " "));
    std::cout << std::endl;
    ++it;
}

您不想第一个,因为您的向量不包含对,您不需要基于it1和end1循环,因为它们表示您传递复制的范围。

You don't want first because your vector doesn't contain pairs, and you don't need to loop based on it1 and end1 because they denote the range you pass to copy.

这篇关于打印2d矢量的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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