I've finally told myself that I'm going to learn C++, so I've been using the tutorials on the site, and they're great. I've run into just one problem that I can't seem to find an answer for:
If I create an array using dynamic memory:
1 2
string * line;
line = new string [1];
How can I extend the size of the array while keeping the contents I store in line[0]?
Specifically, my code is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int x = 0;
string * line;
ifstream file(FILENAME);
if(file.is_open())
{
while(!file.eof())
{
line = new string [++x];
getline(file, line[x-1]);
}
file.close();
for(int a=0;a<x;a++)
{
cout << line[a] << endl;
}
}
I want to be able to keep resizing the array to be just as large as I need. I'm aware that I could get the size of the file before reading in the lines. I'm just picky and want to know how to do it this way.
Use an STL container such as vector<> or deque<> instead. (Ultimately these classes do exactly what you are asking anyway; they just keep you from having to worry about the details).