Search code examples
c++scanfistream

overriding `istream operator>>` vs using `sscanf`


Say I wanted to initialise a std::vector of objects e.g.

class Person { int ID; string name; ...}

from a file that contains a line for each object. One route, is to override operator>> and simply std::cin>>temp_person, another - which I used to favour is to use sscanf("%...", &...) a bunch of temporary primitive types and simply .emplace_back(Person(temp_primitives...).

Which way achieves the quickest runtime ignoring memory footprint? Is there any point in mmap()ing the entire file?


Solution

  • Since you are reading from a file, the performance is going to be I/O-bound. Almost no matter what you do in memory, the effect on the overall performance is not going to be detectable.

    I would prefer the operator>> route, because this would let me use the input iterator idiom of C++:

    std::istream_iterator<Person> eos;
    std::istream_iterator<Person> iit(inputFile);
    std::copy(iit, eos, std::back_inserter(person_vector));
    

    or even

    std::vector<Person>   person_vector(
        std::istream_iterator<Person>(inputFile)
    ,   std::istream_iterator<Person>()
    );