By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,869 Members | 1,742 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,869 IT Pros & Developers. It's quick & easy.

What the fastest way to copy strings from file to a vector?

P: n/a
What the fastest way to copy strings from file to a vector in STL?
Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"SIgnOff" <Si*****@gala.net> wrote...
What the fastest way to copy strings from file to a vector in STL?


In what sense "fastest"? And a vector of what? Assuming certain things,
one could write

std::vector<std::string> vs;
std::string somestring;
std::ifstream somefile("somefilename");
while (std::getline(somefile, somestring))
vs.push_back(somestring);

V
Jul 23 '05 #2

P: n/a
Check out boost serialization

http://www.rrsd.com/boost/libs/seria...doc/index.html

Raj

Jul 23 '05 #3

P: n/a
Victor Bazarov wrote:
"SIgnOff" <Si*****@gala.net> wrote...
What the fastest way to copy strings from file to a vector in STL?

In what sense "fastest"? And a vector of what? Assuming certain things,
one could write

std::vector<std::string> vs;
std::string somestring;
std::ifstream somefile("somefilename");
while (std::getline(somefile, somestring))
vs.push_back(somestring);


Oh, come on, Victor, you're not going to use an istream_iterator? :-)

std::vector<std::string> vs;
std::ifstream somefile("somefilename");
std::copy(std::istream_iterator<std::string>(somef ile),
std::istream_iterator<std::string>(),
std::back_inserter(vs));
Jul 23 '05 #4

P: n/a
red floyd wrote:

[ ... ]
std::vector<std::string> vs;
std::string somestring;
std::ifstream somefile("somefilename");
while (std::getline(somefile, somestring))
vs.push_back(somestring);


Oh, come on, Victor, you're not going to use an istream_iterator? :-)

std::vector<std::string> vs;
std::ifstream somefile("somefilename");
std::copy(std::istream_iterator<std::string>(somef ile),
std::istream_iterator<std::string>(),
std::back_inserter(vs));


Note that these do _completely_ different things. Victor's code reads
each _line_ in the input into a string. Your code reads each _word_
into its own string.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 23 '05 #5

P: n/a
Jerry Coffin wrote:
red floyd wrote:

[ ... ]

std::vector<std::string> vs;
std::string somestring;
std::ifstream somefile("somefilename");
while (std::getline(somefile, somestring))
vs.push_back(somestring);


Oh, come on, Victor, you're not going to use an istream_iterator? :-)

std::vector<std::string> vs;
std::ifstream somefile("somefilename");
std::copy(std::istream_iterator<std::string>(som efile),
std::istream_iterator<std::string>(),
std::back_inserter(vs));

Note that these do _completely_ different things. Victor's code reads
each _line_ in the input into a string. Your code reads each _word_
into its own string.


Good point. You're absolutely right. However, the original post
said "read strings into a vector". Either approach could be correct,
but I suspect OP meant newline delimited. So I concede the point.

I guess if you wanted to use the std::copy method, you'd have to define
a proxy class with operator >> that read an entire line, and also had an
"operator std::string() const" method. At which point we've long since
passed the point of diminishing returns.
Jul 23 '05 #6

P: n/a
red floyd wrote:

[ ... ]
Note that these do _completely_ different things. Victor's code
reads each _line_ in the input into a string. Your code reads
each _word_ into its own string.

Good point. You're absolutely right. However, the original post
said "read strings into a vector". Either approach could be correct,
but I suspect OP meant newline delimited. So I concede the point.


No point to concede, IMO. I wasn't trying to say either was right or
wrong, just that they're not the same. I don't think the OP provided
enough information to indicate which is desired.
I guess if you wanted to use the std::copy method, you'd have to
define a proxy class with operator >> that read an entire line,
and also had an "operator std::string() const" method. At which
point we've long since passed the point of diminishing returns.


There's another possibility -- imbue the stream with a locale that
defines the newline as its only space character. I'd tend to agree that
neither of these is practical unless you use them considerably more
than once.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 23 '05 #7

P: n/a
These codes widely well-known both.
Is any other fast ways?

-----------------
std::vector<std::string> vs;
std::ifstream somefile("somefilename");
std::copy(std::istream_iterator<std::string>(somef ile),
std::istream_iterator<std::string>(),
std::back_inserter(vs));
---------------
std::vector<std::string> vs;
std::string somestring;
std::ifstream somefile("somefilename");
while (std::getline(somefile, somestring))
vs.push_back(somestring);
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.