Will this method involve memory reallocation and thus influence its efficiency?

Viewed 103

I got to find that we could read the contents of a file into a std::vector like this:

  ifstream fin(..., ios::in);
    std::vector<char> buf(
            std::istreambuf_iterator<char>(fin), 
            std::istreambuf_iterator<char>());   

Will this method cause plenty of memory reallocation like when I call buf.push_back(); for many times? What is the fastest or best method to read a file into a std::vector?

Edit: By the way, I find there is a method to read a file into a stringstream:

stringstream ss;
ifstream fin(..., ios::in);
fin >> ss.rdbuf();

Will this method have same problem of memory reallocating ?

2 Answers

std::istreambuf_iterator is an input iterator, so the distance between begin and end is not known in advance. There will be several reallocations during the constructor, unless the file is very small. For a random access iterator the distance would be known and in such case the constructor could avoid the extra memory allocations.

If you roughly know the size of the file, you can use reserve() before reading:

std::vector<char> buf;
buf.reserve(file_size);
buf.insert(buf.end(), std::istreambuf_iterator<char>(fin), 
        std::istreambuf_iterator<char>());   

Vector keeps the data allocated sequentially. When a new element is added it may have no memory free after the last element, then it need to move all the data to a place in memory where it has enough room to the old and new data.

The best solution is give a buffer to vector with the follow command: vector::reserve(size);

Your code could be:

std::vector<char> buf;
buf.reserve(10000);
buf.assign(std::istreambuf_iterator<char>(fin),
            std::istreambuf_iterator<char>());
buf.shrink_to_fit(); //free the unused memory
Related