c++ - 二维 vector 的打印内容

标签 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>::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;
    }

这是我得到的编译错误:

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'

关于如何修复它以便我可以打印测试内容的任何建议

最佳答案

我认为这更符合您的要求。

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

你不需要 first 因为你的 vector 不包含对,你不需要基于 it1 和 end1 循环,因为它们表示你传递给复制的范围。

关于c++ - 二维 vector 的打印内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10368177/

相关文章:

c++ - Win32 C++ sendto() 返回错误 8 : WSA_NOT_ENOUGH_MEMORY

c++ - 使用 cvWarpPerspective 变形图像导致图像的某些部分超出可视区域

C++ Eigen 抛出错误但完成计算

c++ - 是从动态分配的数组索引超出范围的元素有效

c++ - 优化器和专用于 what 命令的字符串之间的交互

C++:将结构的类型更改为子类型

c++ - std::find的替代方法,它返回所有找到的值,而不是仅存在重复项的vector的第一个

c++ - C++:如何正确使用#include <协程>中可用的generator <>,task <>和lazy <>类?

c++ - 是否可以克隆多态对象而无需手动将重写的克隆方法添加到 C++ 中的每个派生类中?

c++ - 为什么派生构造函数被迫在 C++ 中调用基构造函数?