c++ - Reading file line by line WITHOUT using FOR/WHILE loop -
hey guys here's interesting challenge all. i'm given text file , i'm supposed process information line line. processing part trivial long can obtain individual lines. here's challenge:
- i must without using for/while loops in code. (this include recursions)
- i allowed use standard c++ library.
currently right best solution this: is there c++ iterator can iterate on file line line? i'm hoping better 1 not involve creating own iterator class or implementing proxy std::string.
p.s. school assignment , challenge here solve problem using combination of std functionalities , algorithms have no clue how go solving it
ifstream input("somefile") if (!input) { /* handle error */ } //mydatatype needs implement operator>> std::vector<mydatatype> res; std::istream_iterator<mydatatype> first(input); std::istream_iterator<mydatatype> last; std::copy(first,last, std::back_inserter(res)); //etc..
your input operator can this:
std::istream& operator>>(std::istream &in,mydatatype & out) { std::string str; std::getline(in,str); //do str without using loops return in; }
there lot of loops here (you dont' want use goto
, don't you?), hidden behind std::copy
, std::getline
Comments
Post a Comment