Search code examples
c++multidimensional-arrayboost-multi-array

cpp multidimensional vector


I want to store theses string vectors as a 4 dimensional vector. It has been three days that I am searching and I can not decide wether use multidimensional vector,boost multi array ,array of struct ,... I am so new to cpp and they are so confusing.

vector < string >ID;
vector < string > firstName;
vector < string > lastName;
vector < string > address;
vector<vector<vector<vector<string> >>> person ;

what should I do for populating person ?


Solution

  • In your case, there is no point in creating multidimensional array. I'd rather try:

    class Person
    {
    public:
        string Id;
        string firstName;
        string lastName;
        string address;
    };
    
    (...)
    
    vector<Person> People;
    
    // Adding
    Person p;
    p.Id = "1234";
    People.push_back(p);
    
    // Count
    std::cout << "Currently you have " << People.size() << " people in the database";
    
    // Access
    Person p1 = People[0];
    

    Edit: In response to comments

    It's quite tough to answer the question without some specifics about your problem. From what little I know about it, I'd probably go into multi-class version:

    class Id
    {
    public:
        int Value;
        std::vector<FirstName> Names;
    }
    
    class FirstName
    {
    public:
        string Value;
        std::vector<SecondName> SecondNames;
    }
    
    class SecondName
    {
    public:
        string Value;
        std::vector<Address> Addresses;
    }
    
    class Address
    {
    public:
        string Value;
    }