C ++ Tokenizing using iterators in an eof () loop

I am trying to adapt this answer

How to make string tokenization in C ++?

to my current line problem, which involves reading from a file before eof.

from this source file:

Fix grammatical or spelling errors

Clarify meaning without changing it

Correct minor mistakes

I want to create a vector with all the tokenized words. Example: vector<string> allTheText[0] should be "Fix"

I don’t understand the purpose istream_iterator<std::string> end;, but I included the reason that he was on the original poster.

So far, I have this non-working code:

vector<string> allTheText;
          stringstream strstr;
          istream_iterator<std::string> end;
          istream_iterator<std::string> it(strstr);

          while (!streamOfText.eof()){
                getline (streamOfText, readTextLine);
                cout<<readTextLine<<endl;

                stringstream strstr(readTextLine);
                // how should I initialize the iterators it and end here?

                }

Edit:

I changed the code to

          vector<string> allTheText;
          stringstream strstr;
          istream_iterator<std::string> end;
          istream_iterator<std::string> it(strstr);

          while (getline(streamOfText, readTextLine)) {
               cout << readTextLine << endl;

        vector<string> vec((istream_iterator<string>(streamOfText)), istream_iterator<string>()); // generates RuntimeError


          }

And got a RuntimeError, why?

+3
source share
1 answer

Using a loop while (!….eof())in C ++ does not work, because the loop will never exit when the thread goes into an error state!

, . , :

while (getline(streamOfText, readTextLine)) {
    cout << readTextLine << endl;
}

. ? - ?

. copy , .

vector<string> vec((istream_iterator<string>(cin)), istream_iterator<string>());

, , .

EDIT , :

++ . - , , . ++ [ a, b [. , ( , , - ). a . , b, . ? :

for (Iterator i = a; i != b; ++i)
    cout << *i;

, , *. .

++ (, vector, list) , . , . , C- :

int values[3] = { 1, 2, 3 };
vector<int> v(values, values + 3);

values &values[0], , . values + 3, , &values[3] ( ++!) .

, . - , . , ++. iterator , ++ , * . (, string ).

, . , ( , , !). istream_iterator. , begin . , ( cin).

:

istream_iterator<string> front(cin);
istream_iterator<string> back;

vector<string> vec;

for (istream_iterator<string> i = front; i != back; ++i)
    vec.push_back(*i);

, , :

string word;
while (cin >> word)
    vec.push_back(word);
+9

Source: https://habr.com/ru/post/1702329/


All Articles