Thread: count how many lines, allocate vector, and read again

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    48

    count how many lines, allocate vector, and read again

    dear there,
    I need to read a large file line by line into a vector, and it is inefficient to let vector resize itself as needed. so i would like to count how many lines are there, reserve the vector size, and read from the beginning of the file line by line again.
    can anyone give me an idea how to count, and how to move point to beginning of the file again? Thanks.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The rewind() function resets the file position to the start.

    Although, with a smart standard library I'll contend that reading the file twice is not necessarily faster than letting the vector resize, especially if you give it a good initial size. For example, Microsoft's STL in VS 2008 is smart, and will not create copies of all the strings during a resize. So is GCC's STL in 4.3 and higher, if you enable C++0x features (rvalue references).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If the file is very large, it is extremely likely that it will take longer to read it twice.

    Also, if you do your own resizing (e.g. reserve double the size when you run out of current size), you can reduce the number of times the vector is resized.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Vector usually doubles its size anyway.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> it is inefficient to let vector resize itself as needed
    As indicated by the other posters, this may not be true. Did you profile it or are you just making that assumption?

Popular pages Recent additions subscribe to a feed

Tags for this Thread