开发者

C++ a class with an array of structs, without knowing how large an array I need

开发者 https://www.devze.com 2022-12-23 23:32 出处:网络
I have a class with fields like firstname, age, school etc. I need to be able to store other information like for instance, where they have travelled, and what year it was in. I cannot declare another

I have a class with fields like firstname, age, school etc. I need to be able to store other information like for instance, where they have travelled, and what year it was in. I cannot declare another class specifically to hold travelDestination and what year, so I think a struct might be best. This is just an example:

struct travel {
    string travelDest;
    string year;
};

The issue is people are likely to have travelled different amounts. I was thinking of just having an array of travel structs to hold the data. But how do I create a fixed sized array to hold them, without knowing how big I need it to be?

Perhaps I am going about this the completely wrong way, so any suggestions as开发者_StackOverflow to a better way would be appreciated.


I realise there is essentially no difference between a class and struct, but for the purposes of assignment criteria I am not allowed a "class", so yeah.


You could try associating a std::vector with each person, with each entry in the vector containing a struct:

typedef struct travel {
    string travelDest;
    string year;
} travelRecord;

std::vector<travelRecord> travelInfo;

You can then add items to the vector as you see fit:

travelRecord newRecord1 = {"Jamaica", "2010"};
travelInfo.push_back(newRecord1);

travelRecord newRecord2 = {"New York", "2011"};
travelInfo.push_back(newRecord2);

Some more information about vector operations can be found here.


Try learning about the C++ Standard Template Library (STL). For example, you could use a list.


You probably want to use one of the STL container classes to hold your struct (or better yet, refactor your struct into a class to hide the data members and make get/set accessors)

std::vector

std::list

...

Which one you pick depends entirely on what operations you anticipate doing on them. A vector is fine for basic storage, and random access. If you want to perform any sorting or searching functions, you probably want a list, or potentially even a map or multimap.

Here's a good starting point for more information on STL: http://www.parashift.com/c++-faq-lite/class-libraries.html


I would agree with the others, use an stl container like list. A list (or any other container) will grow as you push data onto it, like this:


#include <list>
#include <string>
using namespace std;

struct travel {
    string travelDest;
    string year;
};

int main() {
    list<travel> travels;
    travel t = {"Oslo", "2010"};
    travels.push_back(t);
}

Also note that while you are saying you cannot create a class, you are perfectly willing to create a struct. In C++, these are for all practical purposes the exact same thing, except that struct variables are public by default, while class variables are private by default.


To create an array of 100 elements you can do this:

travel* pTravelArray = new travel[100];

You can make that variable

int numberOfElements = 100;
travel* pTravelArray = new travel[numberOfElements];

Just don't forget to get rid of it when you're done:

delete [] pTravelArray;

As other have suggested though, the STL library has some much nicer (less easy to do something stupid with memory allocation) collections in it you can use.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号