I want to create a program that will keep the records of a number of people. I have created a struct Person_t. Now I want to create an Array that will hold the data about the person, but of indefinite size. I can't use a dynamic array because the size will be limited, excessive memory will be consumed!
First off, how much data (approximately) are you intending to store?
Second, do you know how to use C++ templates? There are two templates in the standard library that would be of some help to you...
1 2 3 4 5
#include <vector>
std::vector<type> name;
name.push_back(/*element*/); //Adds a copy of the argument to the end of the vector.
name[/*location*/]; //Like [] for arrays.
name.size(); //Gets the number of elements stored.
1 2 3 4 5
#include <deque>
std::deque<type> name;
name.push_back(/*element*/); //Adds a copy of the argument to the end of the deque.
name[/*location*/]; //Like [] for arrays.
name.size(); //Gets the number of elements stored.
Both of them are rather the same in terms of interface, but in terms of implementation they are quite different and thus each supports a few extra features.
vector.reserve(); //This can speed up push_back()s if used properly.
deque.push_front(); //Like push_back(), except from the other side.
See the reference page of this site for more details. :)
error C2143: syntax error : missing ';' before '.'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2371: 'People' : redefinition; different basic types
I haven't used the word People any where else apart from this part of the code.
Person_t is defined and works for arrays!
This compiles happily. What are you doing differently?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <vector>
struct People
{
int age;
int shoe_size;
};
int main()
{
std::vector<People> aVectorOfPeople;
aVectorOfPeople.reserve(50);
return 0;
}