istream-iterator

Why doesn't range-for find my overloads of begin and end for std::istream_iterator?

邮差的信 提交于 2019-11-30 15:07:22
问题 I have code like this std::ifstream file(filename, std::ios_base::in); if(file.good()) { file.imbue(std::locale(std::locale(), new delimeter_tokens())); for(auto& entry : std::istream_iterator<std::string>(file)) { std::cout << entry << std::endl; } } file.close(); where std::istream_iterator<std::string> 's begin() and end() are defined as follows template<class T> std::istream_iterator<T> begin(std::istream_iterator<T>& stream) { return stream; } template<class T> std::istream_iterator<T>

Why doesn't range-for find my overloads of begin and end for std::istream_iterator?

我们两清 提交于 2019-11-30 12:55:13
I have code like this std::ifstream file(filename, std::ios_base::in); if(file.good()) { file.imbue(std::locale(std::locale(), new delimeter_tokens())); for(auto& entry : std::istream_iterator<std::string>(file)) { std::cout << entry << std::endl; } } file.close(); where std::istream_iterator<std::string> 's begin() and end() are defined as follows template<class T> std::istream_iterator<T> begin(std::istream_iterator<T>& stream) { return stream; } template<class T> std::istream_iterator<T> end(std::istream_iterator<T>& stream) { return std::istream_iterator<T>(); } which is what Mark Nelson

getline vs istream_iterator

南笙酒味 提交于 2019-11-30 07:42:41
问题 Should there be a reason to preffer either getline or istream_iterator if you are doing line by line input from a file(reading the line into a string, for tokenization). 回答1: I sometimes (depending on the situation) write a line class so I can use istream_iterator : #include <string> #include <vector> #include <iterator> #include <iostream> #include <algorithm> struct Line { std::string lineData; operator std::string() const { return lineData; } }; std::istream& operator>>(std::istream& str

getline vs istream_iterator

匆匆过客 提交于 2019-11-29 05:22:47
Should there be a reason to preffer either getline or istream_iterator if you are doing line by line input from a file(reading the line into a string, for tokenization). I sometimes (depending on the situation) write a line class so I can use istream_iterator : #include <string> #include <vector> #include <iterator> #include <iostream> #include <algorithm> struct Line { std::string lineData; operator std::string() const { return lineData; } }; std::istream& operator>>(std::istream& str,Line& data) { std::getline(str,data.lineData); return str; } int main() { std::vector<std::string> lines(std:

Using a regex_iterator on an istream

折月煮酒 提交于 2019-11-28 14:03:24
I want to be able to solve problems like this: Getting std :: ifstream to handle LF, CR, and CRLF? where an istream needs to be tokenized by a complex delimiter; such that the only way to tokenize the istream is to: Read it in the istream a character at a time Collect the characters When a delimiter is hit return the collection as a token Regexes are very good at tokenizing strings with complex delimiters: string foo{ "A\nB\rC\n\r" }; vector<string> bar; // This puts {"A", "B", "C"} into bar transform(sregex_iterator(foo.cbegin(), foo.cend(), regex("(.*)(?:\n\r?|\r)")), sregex_iterator(), back

Distance between istream_iterators

∥☆過路亽.° 提交于 2019-11-28 12:45:30
I am trying to count the amount of elements read by std::cin by using std::distance for the constructor argument of a vector in advance, like so: // Gives correct amount, but now can't use to put into vector int size = std::distance(std::istream_iterator<std::string>(std::cin), std::istream_iterator<std::string>()); std::vector v(size); // Already read from stream std::copy(...); Obviously I can complete this in one step with std::copy , but that would require setting a size beforehand. My question isn't about vectors though, it's about getting the size of an arbitrary input using std::istream