Hiya.

Many apologies if the question I am about to ask has already been answered. I searched the forum, and found a couple of threads that seemed to be relevant to my problem, but none seemed to describe the problem fully.

I'm fairly new to C++, having only been using it seriously as part of my studies for the past 6 months or so.

My problem is this. I have a class which must contain two arrays of integers, eg:

class myClass
{
public:
     int firstArray[size];
     int secondArray[size];
     readValues();
};

The readValues function takes input from a text file containing a series of integers and should write it into the relevant array using ifstream. The problem is there is no way of knowing how many values will be in this text file beforehand. Having said that, the text file will not change in size during the program operation.

I'm not sure of the best way of approaching this. Is there a way of possibly defining that the array exists within the class, and define it's size later? Or can I define it later, either within the constructor or another function?

You help in solving this problem would be most appreciated.

Many thanks


Richard

The best way to approach that problem is to use <vector>, not C-style arrays. vectors are arrays which will expand as needed to contain however may data objects you want to put into it. Just include <vector> and use std::vector<int> firstArray; . When you want to add an integer just call it's push_back method like this: firstArray.push_back(10); (or replace the 10 with an int variable).


Second-best approach is to use a pointer to the array, and reallocate it as needed (duplicating the efforts of the std::vector class): int* firstArray;

The best way to approach that problem is to use <vector>, not C-style arrays. vectors are arrays which will expand as needed to contain however may data objects you want to put into it. Just include <vector> and use std::vector<int> firstArray; . When you want to add an integer just call it's push_back method like this: firstArray.push_back(10); (or replace the 10 with an int variable).


Second-best approach is to use a pointer to the array, and reallocate it as needed (duplicating the efforts of the std::vector class): int* firstArray;

Many thanks for your quick reply.

I will read up on vectors and try to get that working. The only reason I used arrays for this issue was that I have never come across vectors yet. But it does sound exactly what I am looking for.

Thanks once again for your help

Richard

I have read up on vectors, and they are exactly what I am looking for! I was able to do the following:

std::vector<int> myArray;
...
myArray.reserve(size);
myArray.push_back(values);

Which fixed the issue. Thanks once again!

>>myArray.reserve(size);
Delete that line because the push_back() method will just add more integers to the array. Let vector resize the array as needed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.