1

I need a dynamic array that I don't have to scale(Determine) to a fixed number like the following

string* s;

I have this code so far, but obviously it doesn't work.

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    fstream f;
    f.open("resa.txt");
    string* s;
    int i = 0;
    while (f.good())
    {
        f >> *(s + i);
        i++;
    }
    return 0;
}

This is my task:

Now we change the class definitions a bit. No static arrays can occur anymore. The fact that the arrays instead become dynamic means that some class methods need to be modified, and that some / some of the classes need copy constructors and assignment methods (or superimposed assignment operator). [...]"

This means, that I just can't use data structures.

F. Müller
  • 2,577
  • 6
  • 30
  • 37

1 Answers1

4

It's not automatic, you have to allocate more memory every time you want to resize, copy elements into new array and delete the old one. Fortunately, standard library got you covered with std::vector - an automatically resizable array.

#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

int main()
{
    fstream f;
    f.open("resa.txt");
    string temp;
    std::vector<std::string> s;
    while (f >> temp)
    {
        s.push_back(temp);
    }
    return 0;
}

I also fixed your input reading - see Why is iostream::eof inside a loop condition (i.e. while (!stream.eof())) considered wrong? (applies to good() as well).


Alternatively, you can use std::istream_iterator to initialize vector in one line instead of using loop (credit to Ayxan):

vector<string> s{ istream_iterator<string>{f}, {} }; 
Yksisarvinen
  • 13,037
  • 1
  • 18
  • 42
  • How about `vector s{ istream_iterator{f}, {} };` – Ayxan Haqverdili Aug 15 '20 at 12:04
  • 1
    @Ayxan Added. I wanted to keep it simple (omitted `std::move` for that), but your solution is a cool oneliner :) – Yksisarvinen Aug 15 '20 at 12:14
  • Is there no other way without using data structures ??? – Aboud Al-Salem Aug 15 '20 at 12:40
  • @AboudAl-Salem Any language-level construct that allowed the behavior you want would have to do the same thing one of the standard containers does. If you want to store data you have to have memory to put it in, which means expanding the size of the structure requires either a node-based linked structure or copying the data around, and each has its advantages and disadvantages. – Miles Budnek Aug 15 '20 at 12:56
  • 1
    @AboudAl-Salem Well, you want a data structure - an array of `std::string` (and `std::string` is a data structure itself). If you don't want `std::vector` or another STL container, you will end up reimplementing vector yourself - `new` an array of strings, read next element, `new` bigger array, copy existing elements, `delete` old array, read next element, `new` bigger array... – Yksisarvinen Aug 15 '20 at 13:05
  • Oh well, so I think that's what I'm supposed to do, thank you very much – Aboud Al-Salem Aug 15 '20 at 13:17