What is the best way to make a program that reads the elements from the input until the EOF and returns the container of read elements? This is what I have to do
p.h file:
#pragma once
#include <list>
#include <iostream>
typedef double Element;
template<typename T>
std::list<T> read(std::istream& u);
This is what I tried:
#pragma once
#include <list>
#include <iostream>
typedef double Element;
template<typename T>
std::list<T> read(std::istream& u){
while(u){
std::list<T> l;
l.push_front(u);
return l;
}
}
p.cpp file:
#include "p.h"
#include <iostream>
int main(){
double u;
std::cin>>u;
std::list<double> l=read(u);
}
What exactly do I have to pass as an argument the a main function? I tried passing an std::cin but it doesn't work. It could also be because read function is not defined properly. Also I don't understand what's the point of typedef being double if we are making generic functions.
When you do
l.push_front(u)
you are trying to store theistream
in thelist
. You need to read aT
from the stream and store that instead. Also note that you need to declarel
outside the loop in order to be able to read all values and then returnl
:Then specify the type you want to read from the stream by supplying it as a template parameter: