Creating an Array of indefinite size

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!

So, it there a way to do so?
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. :)

-Albatross
Last edited on
I can't say, since it is just to understand structs better, it may be about 20, or 30 People.

PS. If I use:
1
2
std::vector <Person_t> People;
name.reserve(People.reserve(50);)


I get the following errors:

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!
Yes, but "name" isn't defined in your program. You should strip everything outside the outermost parentheses on line 2, and the parentheses.

-Albatross
Changed it:
1
2
std::vector <Person_t> People;
People.reserve(125);


The error's that appear remain the same!
Last edited on
Odd. Could I see the full code?

EDIT: And by the way, are you working in C or C++? If you're working in C, then of course none of this will work.

-Albatross
Last edited on

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;
  
}
Ah! I was doing it out side the main function (it wasn't inside any function!!)
That solved it.

PS. What is element in name.push_back(/*element*/);
Last edited on
name is a container (for example, a vector or a deque) of elements, so in

name.push_back(/*element*/);

element is an object of the type that goes in the vector.

For example,

1
2
3
4
5
6
7
vector<int> eggs;
int x = 3;
eggs.push_back(x);

deque<People> bananasOnToast;
People somePeopleObject;
bananasOnToast.push_back(somePeopleObject);


That explains it! Thanks a lot for the help...
Topic archived. No new replies allowed.