Re: Parsing Varialve # of Values
Mike Copeland <mrc2323@cox.net> wrote:
My application here is a small part of a large legacy project I'm
trying to upgrade to use modern C++ & STL constructs - from simple C
code I wrote 20+ years ago.
Using C++ streams and strings have many advantages over using the
equivalent C functions. For example, it's very easy to read an entire
line of input into a string regardless of how long that line might be.
However, unfortunately there are disadvantages as well. For instance,
in most systems that I know of, C++ streams are significantly slower than
the C equivalents. This is inconsequential if the reading/writing doesn't
need to be fast, but it can become significant when large amounts of data
need to be read/written as fast as possible (typically C++ streams will be
at least twice as slow as C streams).
Memory consumption will also be sometimes higher, for example in cases
where you simply have data behind a char* that you want to parse. If you
try to use eg. a stringstream to do it, that data will typically be
*copied* to the stringstream object, rather than being parsed in-place,
thus increasing memory consumption (and the copying itself taking time).
Also, there are some situations where it's just outright *simpler* to
use a C standard function over the C++ equivalent. For example, if you
want to parse an integer in ascii format from a char*, it's just simpler
to use std::atoi() than a stringstream. (The reverse is, of course, also
true in other cases, such as when reading a variable-length string from
a file, in which case C++ streams make it much simpler.)
So C++ streams are not a panacea over C streams. Sometimes they are
useful and in fact much handier, sometimes they aren't.
Paul's technique works nicely (now that I understand it), but your
and Victor's discussions have me totally confused. 8<}}
Rather than use an STL algorithm, it may in fact be simpler to just
write a for-loop explicitly (rather than your switch block). The
advantage is that you are not limited to using C++ streams, but instead
you can use eg. std::strtol (which, as said, may be more efficient
depending on the situation).