开发者

How to store an unknown number of elements in an array C++

开发者 https://www.devze.com 2023-02-25 02:35 出处:网络
Sorry if the title is not clear but I ll explain now my problem. I am new in C++. I have created a class in C++. Instances of that class are the input of the program and they have to be stored in an

Sorry if the title is not clear but I ll explain now my problem. I am new in C++.

I have created a class in C++. Instances of that class are the input of the program and they have to be stored in an array to perform the calculations. The problem is that the number of instances of that class that have to be defined by the user is fixed for a single run but can vary from run to run. Here an example:

#include <<blablah>blahblah>

int main()
{
int number_of_instances = 3;

MyClass first_instance(one_parameter_1, another_parameter_1);
MyClass second_instance(one_parameter_2, another_parameter_2);
MyClass third_instance(one_parameter_3, another_parameter_3);

///////////////////

N开发者_StackOverflow中文版OW I HAVE TO STORE ALL THREE IN AN ARRAY LIKE

MyClass array[number_of_instances] = {first_instance, second_instance, third_instance};

THE PROBLEM IS THAT I DO NOT KNOW BEFORE HAND HOW MANY OF THEM ARE THE USER IS GOING TO INPUT

///////////////////

performCalculations(array);
return 0;
}

Thanks a lot in advance.


The typical C++ solution is to use a vector.

vector<MyClass> v;
v.push_back(first_instance);  //add an already instantiated object to the end of the vector
v.push_back(second_instance);
v.push_back(third_instance);

You won't have to worry about memory management and you are able to access the vector like you would a normal array:

v[0].classMember

You can also add items to the vector in a loop if needed like so:

for(int i = 0; i < 5; i++){
    v.push_back( MyClass(i, param2) );
}

And all the objects will be destructed when the vector goes out of scope if you're storing the objects directly in the vector.

One of the downsides to storing the objects directly in the vector is passing the vector as a parameter to a function. This will be a slow operation since the vector (and all the objects it holds) will have to be copied.


If you know the number of instances before you read them all in then you can allocate an array on the heap using new[]. (Don't forget to delete[] them when you've finished.) Note that this requires that the object have a default constructor.


you should use std::vector in this case rather than a built-in array.

#include <vector>
...
std::vector<MyClass> v = {first_instance, second_instance, third_instance};
...
v.push_back(fourth_instance);


If you don't know how many elements the array will contain, I would use a std::vector instead of a plain array as the vector will grow to accommodate the additional elements.


What you want is the Vector class from the standard template library, it behaves like an array but it will re-size itself if you fill it's internal allocation. If you do not need random access to it (i.e. use the [] opperator) you may want to use the List class instead. If you use List you will need to create an enumerator to step through it.


use std::vector<MyClass>, vector template can be found in <vector> header. YOu must learn a little bit to code well, before coding use any of online available c++ FAQs

0

精彩评论

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

关注公众号