4
Contributors
3
Replies
21
Views
3 Years
Discussion Span
Last Post by mike_2000_17
0

You have to iterate through the elements of the vector and output each string to the file.

Here's one way:

std::ofstream file_out("filename.txt");
std::copy(v.begin(), v.end(),
          std::ostream_iterator<std::string>(file_out, "\n"));

Here's another:

std::ofstream file_out("filename.txt");
for(const auto& s : v)
  file_out << s << std::endl;

Here's another:

std::ofstream file_out("filename.txt");
for(auto it = v.begin(); it != v.end(); ++it)
  file_out << *it << std::endl;

Here's another:

std::ofstream file_out("filename.txt");
for(std::size_t i = 0; i < v.size(); ++i)
  file_out << v[i] << std::endl;

There's really not much to it. Pick what you are most comfortable with.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.