简体   繁体   中英

Printing 3D vector

I'm trying to print 3D vector but facing operator not matching issue. 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.

It just doesn't know how to print *i element (which has type 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:

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++

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM