Dynamic arrays

I'm just starting to learn C ++, so excuse me for this simple question. What I'm doing is reading numbers from a file, and then trying to add them to an array. My problem is how to increase the size of the array? For example, I thought I could just do:

#include <iostream>
using namespace std;

int main() {
    double *x;
    x = new double[1];
    x[0]=5;
    x = new double[1];
    x[1]=6;
    cout << x[0] << "," << x[1] << endl;
    return 0;
}

But this, obviously, just overwrites the value 5, which I originally set to x [0], and therefore gives 0.6. How would I make it give out 5.6?

Please understand that for the example that I included, I did not want to clutter it up by reading the code from the file or the code to get numbers from the user. In the actual application, I won’t know how big the array I need at compile time, so please don’t tell me to just create an array with two elements and set them to 5 and 6 respectively.

Thank you for your help.

+5
source share
6 answers

. vector. push_back, , .

#include <iostream>
#include <vector>

int
main() {
    double value;
    std::vector<double> values;

    // Read in values
    while (std::cin >> value) {
        values.push_back(value);
    }

    // Print them back out
    for (std::size_t i(0), len(values.size()); i != len; ++i) {
        std::cout << values[i];
    }
}
+17
+4

, STL , : x = double [2];

, , . , " " (, )... - , . , . , , STL , .

#include <iostream>
using namespace std;
int main() {
    double *x = new double[2];
    x[0]=5;
    x[1]=6;
    cout << x[0] << "," << x[1] << endl;
    return 0;
}
+4

, , :

#include <iostream>
using namespace std;

int main() {
    // Allocate some memory for a double array of size 1 and store
    // an address to the beginning of the memory in mem_address.
    double* mem_address = new double[1];

    // Assign 5 to the first element in the array.
    mem_address[0] = 5;

    // Save the address of the memory mem_address is currently
    // referencing.
    double* saved_address = mem_address;

    // Allocate some memory for a double array of size 2 and store
    // an address to the beginning of the memory in mem_address.
    mem_address = new double[2];

    // Copy over the 1 element from the first memory block
    // to the new one.
    mem_address[0] = saved_address[0];

    // Done with the old memory, so clean it up.
    delete [] saved_address;

    // Assign 6 to the second element in the new array.
    mem_address[1] = 6;

    // Print out the 2 elements in the new array.
    cout << mem_address[0] << "\n";
    cout << mem_address[1] << "\n";

    // Done with the new array memory now, so clean it up.
    delete [] mem_address;
}
+3

- STL - , , :

, :

int *a = malloc(int * ARBITRARY_SIZE);
int size = 0;
int allocated = ARBITRARY_SIZE;

, , "". ARBITRARY_SIZE, "" 2 . [size].

void addElement(int value) {
  ++size;

  if (size == allocated) {
    allocated *= 2;
    a = realloc(sizeof(int) * allocated);
    a = new_a;
  }

  a[size] = value;
}

, : x [1] .

, , malloc realloc null.

+2

. , , , , . , 2531 .

The problem with redistribution is that it can be an expensive operation. Therefore, if you need to add 5 more elements to an array of 5000 elements, you can finish copying all 5000 elements in memory.

Using a linked list instead can be considered for such a scenario.

0
source

All Articles