distance(istream_iterator<Word> in(cin), istream_iterator<Word> eof) funktioniert nicht
-
Hallo, wer kann mir helfen. Ich möchte die Variante 3 (im Listing: main.cpp) zum Laufen bringen. Ich find den Fehler leider einfach nicht. Irgendwie bleibt das Programm in einer Endlosschleife hängen, Abbruch mit Control-Z (wie vorgesehen) geht nicht.
Die Variante 1 funktioniert..
Natürlich könnte die Methode read() auch anders aussehen. Ich würde jedoch gerne meine Lösung mit den Iteratoren verwenden.
Vielen Dank bereits jetzt! Grüsse aus der Schweiz, Thomas
main.cpp
#include <iostream> #include <iterator> #include <algorithm> #include <vector> #include "Word.h" using namespace std; int main() { // Variante 1: funktioniert // ======================== Word word1, word2, word3; cin >> word1 >> word2 >> word3; cout << word1 << word2 << word3; // Variante 2: funktioniert nicht // ============================== // // vector<Word> v; // istream_iterator<Word> eof; // for (istream_iterator<Word> it(cin); (it != eof); ++it) { // v.push_back(*it); // } // // cout << distance(v.begin(), v.end()) << endl; // Variante 3: funktioniert nicht // ============================== // // istream_iterator<Word> eof; // cout << distance(istream_iterator<Word>(cin), eof) << endl; return 0; }Word.h
#ifndef WORD_H_ #define WORD_H_ #include <string> #include <iostream> using std::string; class Word { private: string word; public: Word(); Word(string w); void read(std::istream&); friend std::istream& operator>>(std::istream&, Word&); friend std::ostream& operator<<(std::ostream&, Word&); }; #endif /*WORD_H_*/Word.cpp
#include <string> #include <cctype> #include <iterator> #include "Word.h" using namespace std; Word::Word() : word("") {} Word::Word(string w) : word(w) {} void Word::read(istream& in) { istreambuf_iterator<char> it(in); while(!isalpha(*it)) it++; while(isalpha(*it)) { word.push_back(*(it++)); } } istream& operator>>(istream& input, Word& word) { word.read(input); return input; } ostream& operator<<(ostream& output, Word& word) { output << word << endl; return output; }//Post in den reg nick gepackt und den unreg post gelöscht.

-
Ich dachte gerade ich hätte es... leider doch nicht.

Wer kann mir helfen?void Word::read(istream& in) { istreambuf_iterator<char> it(in); istreambuf_iterator<char> eof; while(!isalpha(*it)) it++; while(isalpha(*it) && (it != eof)) { word.push_back(*(it++)); } }