In: Computer Science
Please answer in C++ Thanks! Use an istream_iterator, the copy algorithm and a back_inserter to read the contents of a text file that contains int values separated by whitespace. Place the int values into a vector of ints. The first argument to the copy algorithm should be the istream_iterator object that's associated with the text file's ifstream object. The second argument should be the istream_iterator object that's initialized using the class template istream_iterator's default constructor-- the resulting object can be used as an "end" iterator. After reading the file's contents, display the contents of the resulting vector.
Implemetation of above program in C++
Please hit that like button or thumbs-up,it really motivates me.>3
Program.cpp
// Incliding needed standard libraries
// You can use <bits/stdc++.h> to inclue all libraries without one by one.
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <fstream>
using namespace std;
int main()
{
std::vector< int > showVector; //Making a vector
std::ifstream inputFile("E:/a.txt", std::ios::in); // Reading a.txt from location E:/a.txt
std::istream_iterator<int> inputFromFile(std::cin); //
istream_iterator, you can use inputFile on the place of
std::cin
// Copy file content to vector list
std::copy(inputFromFile, std::istream_iterator< int
>(),back_inserter(showVector ));
for ( int i=0;i<showVector.size(); i++) // Until size of
vector
std::cout << showVector[i] << "\t"; // Printing
Result
std::cout << std::endl;
return 0;
}
And Please use a proper C++ IDE to get desired output and also give read permissions.
And do not foeget to hit that like button>3
Thank you!!