Q:

Write a program using stream iterators to read a text file into a vector of strings

0

Write a program using stream iterators to read a text file into a vector of strings.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <iterator>
#include <vector>
#include <string>
#include <fstream>
#include <iostream>

int main() {
  std::string filename;
  std::cin >> filename;
  std::ifstream in(filename);
  if (!in.is_open()) {
    std::cerr << "Can not open file: " << filename << std::endl;
    return -1;
  }
  std::istream_iterator<std::string> iter(in), eof;
  //std::vector<std::string> vs;
  //while (iter != eof)
  //  vs.push_back(*iter++);
  std::vector<std::string> vs(iter, eof);  // The shorter way
  for (const auto &s: vs)
    std::cout << s << std::endl;

  return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now