Вопрос

This isn't working. And the reason why is cryptic to me.

PS: Using C++11

http://ideone.com/elopRc

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

using namespace std;

int main() {
    struct MyStruct {
        size_t some_num;
        char some_char;
        bool some_bool;
        MyStruct* some_ptr;
    };

    vector<vector<vector<MyStruct>>> three_d_struct_v;

    size_t max_i = 100;
    size_t max_j = 10;
    size_t max_k = 10;

    for(size_t i = 0; i < max_i; i++) {
        for(size_t j = 0; j < max_j; j++) {
            for(size_t k = 0; k < max_k; k++) {
                three_d_struct_v.emplace_back(k, 'x', false, nullptr);
            }
        }
    }


    return 0;
}
Это было полезно?

Решение

Here, three_d_struct_v is of type vector<vector<vector<MyStruct>>>, i.e. a vector of vector<vector<MyStruct>>, so you need to add element of type vector<vector<MyStruct>> (likewise for nested dimensions). However, in your code, you are adding element of type MyStruct directly.

You need to change to something like:

for(size_t i = 0; i < max_i; i++) {
    vector<vector<MyStruct>> v1;
    for(size_t j = 0; j < max_j; j++) {
        vector<MyStruct> v2;
        for(size_t k = 0; k < max_k; k++) {
            MyStruct a = {k, 'x', false, nullptr};
            v2.emplace_back(move(a));
        }
        v1.emplace_back(move(v2));
    }
    three_d_struct_v.emplace_back(move(v1));
}

Check out ideone for the whole code.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top