2D 矢量的打印内容
printing contents of 2d vector
这是我正在运行的代码:
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;
}
您不需要首先,因为您的向量不包含对,并且您不需要基于 it1 和 end1 进行循环,因为它们表示您传递到复制的范围。
代码有两个问题。
首先std::vectors
不包含std::pairs
,所以没有first
或second
:
while (it!=end) {
std::vector<double>::iterator it1=it->begin(),end1=it->end();
其次,对std::copy
的调用需要一个范围,该范围可能对应于您的内部向量之一。所以你走得太深了。
您可以遍历外部向量test
,然后使用copy
打印其每个元素(即向量)。
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();
for ( it!= end, ++it) {
std::copy(it1-begin(),it->end(),std::ostream_iterator<double>(std::cout, " "));
}
相关文章:
- 库特<<恩德尔;不适用于打印 2D 阵列
- 如何打印 2D 字符数组C++
- 在C++中初始化和打印 2D 数组
- 打印 2D 动态数组 c++ 函数
- 我无法打印 2D 矢量
- 如何在GDB中按行打印2D数组
- 使用 auto 打印 2D 矢量的内容
- 有没有办法更快地打印2D阵列
- 在C++中打印 2D 字符数组
- 在C++使用新方法打印2d阵列
- 打印2D阵列时内存泄漏
- 是否有任何打印2D阵列C 的快捷方式
- C 打印2D数组
- 打印2D std ::字符串数组
- 打印2D矢量指针
- C++类和函数的问题-打印2D数组的函数
- 如何在C++中打印2D数组
- 如何通过指针打印 2D 阵列?C++
- 使用1d阵列打印2d阵列
- 用c++打印2d阵列