C++ 二维向量的打印内容

C++ 二维向量的打印内容,c++,C++,这是我正在运行的代码: 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

这是我正在运行的代码:

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;
    }
std::向量测试;
测试。推回(标准::向量(30));
std::vector::iterator it=test.begin(),end=test.end();
while(it!=结束){
迭代器it1=it->first.begin(),end1=it->first.end();
while(it1!=end1){
std::copy(it1.begin()、it1.end()、std::ostream_迭代器(std::cout,“”);
++it1;
}
++它;
}
这是我得到的编译错误:

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'
data.cpp:33:45:错误:“class std::vector”没有名为“first”的成员
data.cpp:33:68:错误:“class std::vector”没有名为“first”的成员
data.cpp:35:16:错误:“class std::vector::iterator”没有名为“begin”的成员
data.cpp:35:28:错误:“class std::vector::iterator”没有名为“end”的成员
data.cpp:35:34:错误:“ostream_迭代器”不是“std”的成员
data.cpp:35:56:错误:应在“double”之前使用主表达式

关于如何修复它以便打印测试内容的任何建议该代码有两个问题

First
std::vectors
不包含
std::pairs
,因此没有
First
second

while (it!=end) {
  std::vector<double>::iterator it1=it->begin(),end1=it->end();

代码有两个问题

First
std::vectors
不包含
std::pairs
,因此没有
First
second

while (it!=end) {
  std::vector<double>::iterator it1=it->begin(),end1=it->end();

我想这是你想要的

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;
}
std::向量测试;
//将一些实际数据放入向量的测试向量中
对于(int i=0;i<5;++i)
{
std::向量随机_;
对于(int j=0;j<1+i;++j)
{
随机填充。向后推(静态投射(rand())/rand\u MAX);
}
测试。推回(随机的东西);
}
std::vector::iterator it=test.begin(),end=test.end();
while(it!=结束)
{
std::vector::迭代器it1=it->begin(),end1=it->end();
std::copy(it1,end1,std::ostream_迭代器(std::cout,“”);

我想这是你想要的

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;
}
std::向量测试;
//将一些实际数据放入向量的测试向量中
对于(int i=0;i<5;++i)
{
std::向量随机_;
对于(int j=0;j<1+i;++j)
{
随机填充。向后推(静态投射(rand())/rand\u MAX);
}
测试。推回(随机的东西);
}
std::vector::iterator it=test.begin(),end=test.end();
while(it!=结束)
{
std::vector::迭代器it1=it->begin(),end1=it->end();
std::copy(it1,end1,std::ostream_迭代器(std::cout,“”);

std::cout次要建议:由于您不是通过迭代器修改2D向量,而是只读取其元素,因此请使用
std::vector::const_迭代器
@BojanKomazec,谢谢您的建议。我将提出修正次要建议:由于您不是通过迭代器修改2D向量,而只是读取其元素,请使用
std::vector::const_iterator
@BojanKomazec,谢谢你的建议。我将进行修复感谢推理。出于某种原因,for循环产生编译错误,并将其更改为while works。感谢推理。出于某种原因,for循环产生编译错误,并将其更改为while works。