0

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 ?

user1876128
  • 91
  • 14
  • 12
    What about `class person { /*...*/ };` – hansmaad Nov 27 '13 at 09:28
  • 2
    A multi-dimension vector is required if for instance you have multiple firstnames for an id and multiple lastnames for a firstname and multiple addresses for each lastname. If you just want to store the details of a person, then @hansmaad's suggestion is most suitable. – Abhishek Bansal Nov 27 '13 at 09:31
  • Ok... so what the actual problem is? You want us to decide for you, which data structure should you use? – Spook Nov 27 '13 at 13:26

3 Answers3

2

As suggested by @hansmaad

The following would be a simpler and better implementation(compared to mulch-dimensional vectors) for storing personal details in your program.

Define a person as:

struct person {
    std::string id;
    std::string first_name;
    std::string last_name;
    std::string address;
};

And define your vector as:

std::vector< person > people;
HAL
  • 3,888
  • 3
  • 19
  • 28
1

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;
}
Spook
  • 25,318
  • 18
  • 90
  • 167
0

structure of arrays may be ! Or for a better readable and changeable code use a class

chouaib
  • 2,763
  • 5
  • 20
  • 35