This is a sequel to a previous thread (http://www.cplusplus.com/forum/beginner/205575/) which involved using getline() with cin.ignore () to store an arbitrary amount of number and its correspondng string, afterwhich its the data stored data is displayed.
while (getData(number, buffer))
{
// Store each data input
pNumber[index] = number;
pString[index] = buffer;
index++;
if (index == arraySize)
{
arraySize++; // i.e. increase the array size
double* nTemp = newdouble[arraySize]; // A fresh array is declared with a
string* sTemp = new string[arraySize]; // corresponding increase in array dimension
for (size_t i{}; i < index; ++i)
{
*(nTemp + i) = *(pNumber + i); // Copies existing and new entries
*(sTemp + i) = *(pString + i);
}
delete[] pNumber; // frees up memory storage
delete[] pString;
// Reburse data
pNumber = nTemp;
pString = sTemp;
}
}
At the end of main() (line 68) there should be a pair of delete [] statements to release the allocated memory. Though it doesn't cause a problem here (the program is ending in any case), remembering to do so is a good practice, so you remember to do so in other circumstances where it may be more important.
Having got this far, you are in effect replicating the functionality of the std::vector. Depending on what your goals are, you might consider modifying the code to use vectors instead of multiple new/delete.
You could also consider using a struct or class to keep the string and double together in a single object, again depending on what are your goals.
@Chervil
Thanks once again for your invaluable suggestions. I really appreciate it. I was thinking, could I create a set() function where I separate the code meant to store the data into the array by calling the set() function?
Also, if I do place the variable inside the for loop, doesnt it mean that for each loop there will have to b a new declaration of the temporary variables:
1 2
double* nTemp{};
string* sTemp{};
.
Thirdly, the lines:
1 2 3 4 5
for (size_t i{}; i < index; ++i)
{
*(nTemp + i) = *(pNumber + i); //Copies existing and new entries
*(sTemp + i) = *(pString + i);
}
to be candid it appears as though for each entry of a pair of number and string, a fresh copying (assignment) is made replacing the last pair of data from a previous storage. Is this the case? If so, how could I modify the code structure to be more functional?
Finally, how do u reference code and show their corresponding line numbers? If you would observe, my reference above is incorrect, and I am yet to figure how it should be done.
@Chervil
Thanks once again for your invaluable suggestions. I really appreciate it. I was thinking, could I create a set() function where I separate the code meant to store the data into the array by calling the set() function?
Also, if I do place the variable inside the for loop, doesnt it mean that for each loop there will have to b a new declaration of the temporary variables:
1
2
double* nTemp{};
string* sTemp{};
.
Thirdly, the lines:
48
49
50
51
52
for (size_t i{}; i < index; ++i)
{
*(nTemp + i) = *(pNumber + i); //Copies existing and new entries
*(sTemp + i) = *(pString + i);
}
to be candid it appears as though for each entry of a pair of number and string, a fresh copying (assignment) is made replacing the last pair of data from a previous storage. Is this the case? If so, how could I modify the code structure to be more functional?
Finally, how do u reference code and show their corresponding line numbers? If you would observe, my reference above is incorrect, and I am yet to figure how it should be done.
Also, if I do place the variable inside the for loop, doesnt it mean that for each loop there will have to b a new declaration of the temporary variables:
Yes, that's what I did in the example I posted:
45 46
double* nTemp = newdouble[arraySize]; // A fresh array is declared with a
string* sTemp = new string[arraySize]; // corresponding increase in array dimension
to be candid it appears as though for each entry of a pair of number and string, a fresh copying (assignment) is made replacing the last pair of data from a previous storage. Is this the case? If so, how could I modify the code structure to be more functional?
After the each cin entry, the array size is increased by 1, and all of the existing values are copied to the new array.
That means after entering say 100 values, the whole of those 100 strings and numbers have to be copied to the new array, just to make room for 1 more value. This becomes increasingly inefficient, most of the processing time will be spent doing lots of copying each time.
One way to make this more efficient is to increase the array size in larger steps. For example, you could allocate say 4 elements at the start. That means no reallocation/copying is required until after the 4th entry. Then you could increase the array size to 8, and copy the four existing values. That leaves room for 4 more. When those four are filled, increase the size to 16 and copy the existing 8. That leaves room for another eight. Or choose whatever algorithm you like, this will reduce the amount of copying considerably, since it doesn't need to be done each time, but only after entering a reasonable number of values.
Finally, how do u reference code and show their corresponding line numbers?