Question

Having a simple text based file format for a 3D data structure:

d d d d d
d d d d d
d d d d d
d d d d d
d d d d d
-
s s s s s
s s s s s
s s s s s
s s s s s
s s s s s
-
g g g g g
g g g g g
g g g g g
g g g g g
g g g g g
-

Which represents a 5x5x3 data structure of short strings. I wonder how to efficiently load this data into a std::vector<std::vector<std::vector<std::string>>> type of structure?

I tried this so far:

std::ifstream file("file.txt"); 
std::vector<std::vector<std::vector<std::string>>> data;

int lines = 0;
std::string line;
while(std::getline(file, line)){        
    std::stringstream stream(line);            
    if (line.size() > 1){
        lines++;
        data.resize(lines);            
        data.back().push_back(std::vector<std::string>());
        std::string val;
        while(stream >> val){
            data.back().back().push_back(val);
        }            
    }              
}

Which somewhat works, though only fills up the 3D vector in two dimensions. I am still not sure about the efficiency.

Was it helpful?

Solution 2

The solution became very similar to what I showed in the question. Though with some small modifications:

std::ifstream file("file.txt");
std::vector<std::vector<std::vector<std::string>>> data;

data.push_back(std::vector<std::vector<std::string>>());
std::string line;
while(std::getline(file, line)){        
    std::stringstream stream(line);            
    if (line.size() > 1){
        data.back().push_back(std::vector<std::string>());
        std::string val;
        while(stream >> val){
            data.back().back().push_back(val);
        }            
    } 
    else {
             array_.push_back(std::vector<std::vector<Entity>>());
    }
}

To print the result:

for (auto v1 : data){
    for (auto v2 : v1){
        for(auto s : v2){
            std::cout << s << " ";
        }
        std::cout << std::endl;
    }
    std::cout << std::endl;     
}

Gives:

d d d d d
d d d d d
d d d d d
d d d d d
d d d d d

s s s s s 
... etc

OTHER TIPS

Maybe this code is awful however it does the task.:) Instead of the file I used std::cin.

#include <iostream>
#include <vector>
#include <string>


int main() 
{
    const size_t N = 3;
    const size_t M = 5;

    std::vector<std::vector<std::vector<std::string>>> v;

    v.reserve( N );

    std::vector<std::vector<std::string>> v2;
    v2.reserve( M );
    size_t k = 0;
    do
    {
        v2.clear();

        std::vector<std::string> v1;
        v1.reserve( M );
        size_t j = 0;
        do
        {
            v1.clear(); 
            std::string s;
            for ( size_t i = 0; i < M && std::cin >> s; i++ )
            {
                v1.push_back( s );
            }

            if ( !v1.empty() ) v2.push_back( v1 );
        } while ( ++j < M && !v1.empty() );

        if ( !v2.empty() ) v.push_back( v2 );
    } while ( k < N && ! v2.empty() );

    for ( const std::vector<std::vector<std::string>> &v2 : v )
    {
        for ( const std::vector<std::string> &v1 : v2 )
        {
            for ( const std::string &s : v1 ) std::cout << s << ' ';
            std::cout << std::endl;
        }

        std::cout << std::endl;
    }

    return 0;
}

The output is (the same input is used):

"d" "d" "d" "d" "d" 
"d" "d" "d" "d" "d" 
"d" "d" "d" "d" "d" 
"d" "d" "d" "d" "d" 
"d" "d" "d" "d" "d" 

"s" "s" "s" "s" "s" 
"s" "s" "s" "s" "s" 
"s" "s" "s" "s" "s" 
"s" "s" "s" "s" "s" 
"s" "s" "s" "s" "s" 

"g" "g" "g" "g" "g" 
"g" "g" "g" "g" "g" 
"g" "g" "g" "g" "g" 
"g" "g" "g" "g" "g" 
"g" "g" "g" "g" "g" 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top