M
Marcus Kwok
I am writing a program to read in a file, do some processing, then write
it out to a different file. I really like the idiom I use for output:
// std::vector<std::string> vec;
// std:fstream out;
std::copy(vec.begin(),
vec.end(),
std:stream_iterator<std::string>(out, "\n"));
Is there a way to do the same thing for input that will read whole
lines? I tried the obvious thing:
// std::ifstream in;
std::copy(std::istream_iterator<std::string>(in),
std::istream_iterator<std::string>(),
std::back_inserter(vec));
but it separates on whitespace so every word is a different element in
the vector. I would like it to do the same thing as
// std::string line;
while (std::getline(in, line)) {
vec.push_back(line);
}
It's not a big deal or anything. I would just like the the two
operations to look consistent.
it out to a different file. I really like the idiom I use for output:
// std::vector<std::string> vec;
// std:fstream out;
std::copy(vec.begin(),
vec.end(),
std:stream_iterator<std::string>(out, "\n"));
Is there a way to do the same thing for input that will read whole
lines? I tried the obvious thing:
// std::ifstream in;
std::copy(std::istream_iterator<std::string>(in),
std::istream_iterator<std::string>(),
std::back_inserter(vec));
but it separates on whitespace so every word is a different element in
the vector. I would like it to do the same thing as
// std::string line;
while (std::getline(in, line)) {
vec.push_back(line);
}
It's not a big deal or anything. I would just like the the two
operations to look consistent.