[英]Printing 3D vector
I'm trying to print 3D vector but facing operator not matching issue. 我正在尝试打印3D矢量,但面临操作员不匹配的问题。 This how I fill the vector
这是我如何填充向量
masterList.push_back(tokens)
I tried this code: 我尝试了这段代码:
copy(masterList.begin(), masterList.end(), ostream_iterator<string>(cout, "\n"));
and for look: 和外观:
for( vector<vector<vector<string>>>::const_iterator i = masterList.begin(); i != masterList.end(); ++i)
cout<<*i<<' ';
The latest one does cout
does not print pointer of 3D vector element. 最新的一项
cout
不打印3D向量元素的指针。
It just doesn't know how to print *i
element (which has type vector<vector<string>>
). 它只是不知道如何打印
*i
元素(其类型为vector<vector<string>>
)。 You need two more internal loops to print each string element. 您还需要两个内部循环来打印每个字符串元素。
You can do: 你可以做:
for( vector<vector<vector<string>>>::const_iterator i = masterList.begin(); i != masterList.end(); ++i)
{
for( vector<vector<string>>::const_iterator j = i->begin(); j != i->end(); ++j)
{
for( <vector<string>::const_iterator k = j->begin(); k != j->end(); ++k)
{
cout<<*k<<' ';
}
}
}
Using Boost Spirit karma: 使用Boost Spirit业力:
std::cout << format_delimited(auto_, ' ', v) << "\n";
Prints 版画
a0 a1 a2 a1 a2 a3 a2 a3 a4 a3 a4 a5 b0 b1 b2 b1 b2 b3 b2 b3 b4 b3 b4 b5
std::cout << format(auto_ % ',' % ' ' % eol , v) << "\n";
Prints 版画
a0,a1,a2 a1,a2,a3
a2,a3,a4 a3,a4,a5
b0,b1,b2 b1,b2,b3
b2,b3,b4 b3,b4,b5
std::cout << format_delimited( ("{\n" << ("\t{" << *auto_ << '}') % eol << "\n}\n") % eol, ' ', v) << "\n";
Prints 版画
{
{ a0 a1 a2 }
{ a1 a2 a3 }
}
{
{ a2 a3 a4 }
{ a3 a4 a5 }
}
{
{ b0 b1 b2 }
{ b1 b2 b3 }
}
{
{ b2 b3 b4 }
{ b3 b4 b5 }
}
Or you could use old fashioned range-based for: 或者,您可以将基于范围的老式方法用于:
for (auto& d1 : v) {
for (auto& d2 : d1) {
for (auto& d3: d2)
std::cout << d3 << " ";
std::cout << "\n";
}
std::cout << "\n";
}
Full demo: 完整演示:
#include <vector>
#include <string>
#include <iostream>
#include <boost/spirit/include/karma.hpp>
namespace karma = boost::spirit::karma;
int main()
{
const std::vector<std::vector<std::vector<std::string>>> v { {
{ "a0", "a1", "a2" },
{ "a1", "a2", "a3" },
}, {
{ "a2", "a3", "a4" },
{ "a3", "a4", "a5" },
},
{
{ "b0", "b1", "b2" },
{ "b1", "b2", "b3" },
},
{
{ "b2", "b3", "b4" },
{ "b3", "b4", "b5" },
} };
for (auto& d1 : v) {
for (auto& d2 : d1) {
for (auto& d3: d2)
std::cout << d3 << " ";
std::cout << "\n";
}
std::cout << "\n";
}
using namespace boost::spirit::karma;
std::cout << format_delimited(auto_, ' ', v) << "\n";
std::cout << format(auto_ % ',' % ' ' % eol , v) << "\n";
std::cout << format_delimited( ("{" << *auto_ << "}"), ' ', v) << "\n";
std::cout << format_delimited( ("{\n" << ("\t{" << *auto_ << '}') % eol << "\n}\n") % eol, ' ', v) << "\n";
}
Not sure if this is why you are getting an error, but I know I've had issues when using templates inside of templates like this because you end up with ">>" somewhere in the code which means something very different than likely intended. 不知道这是为什么导致错误,但是我知道在这样的模板中使用模板时遇到了问题,因为您在代码中的某个地方最终出现了“ >>”,这意味着可能与预期的有所不同。
">>" is the bitwise right shift operator in C++ “ >>”是C ++中按位右移运算符
To fix it you just have to put a space between them. 要解决此问题,您只需要在它们之间放置一个空格即可。 For example: instead of this:
例如:代替这个:
vector<vector<vector<string>>>::const_iterator
do this: 做这个:
vector<vector<vector<string> > >::const_iterator
Of course, I haven't tried compiling your code, this is just an observation looking at the example code. 当然,我没有尝试编译您的代码,这只是观察示例代码的一种观察。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.