简体   繁体   English

使用istream_iterator迭代int和string

[英]Iterating over int and string with istream_iterator

I am going through The C++ Programming Language Book and reached "Iterators and I/O" page 61 they give the following example to demonstrate iterating through a string submitted. 我将通过C ++编程语言手册并到达“迭代器和I / O”第61页,他们给出了以下示例来演示迭代提交的字符串。

#include <iostream>
#include <iterator>
#include <string>

using namespace std;

int main()
{

    istream_iterator<string>ii(cin);
    istream_iterator<string>eos;

    string s1 = *ii;
    ++ii;
    string s2 = *ii;

    cout <<s1 << ' '<< s2 <<'\n';
}

Which I totally understand, now I was playing around with this example to make it work for numbers as well. 我完全理解,现在我正在玩这个例子,使它也适用于数字。 I tried adding in the following in the respective places... 我尝试在各自的地方添加以下内容......

istream_iterator<int>jj(cin);
int i1 = *jj;
cout <<s1 << ''<< s2 << ''<< i1 <<'\n';

Which does not give me the chance to input the number section when running the program. 这使我没有机会在运行程序时输入数字部分。 Why is this so ? 为什么会这样? Can the iterator only be used once on cin ? 迭代器只能在cin上使用一次吗? such that it is already has input from cin so the next iterator is ignored ? 这样它已经有来自cin输入所以忽略下一个迭代器?


Edit here is what I have after insertions 这里编辑是我插入后的内容

#include <iostream>
#include <iterator>
#include <string>

using namespace std;

int main()
{

    istream_iterator<string>ii(cin);
    istream_iterator<string>eos;

    //istream_iterator<int>dd(cin);

    string s1 = *ii;
    ++ii;
    string s2 = *ii;
    //int d = *dd;
    int d =24;
    cout <<s1 << ' '<<s2<<' '<<d<< '\n';
}

The above works for 以上工作原理

Hello World or Hello World
Hello 你好
World 世界

Giving Hello World as the output. 给Hello World作为输出。

removing the comments from 删除评论

istream_iterator<int>dd(cin);
int d = *dd;

and commenting out 和评论

int d =24;

Leads to Hello Hello 0 as the output. 导致Hello Hello 0作为输出。

When you first create an istream_iterator, it gets the first input and stores the data internally. 首次创建istream_iterator时,它会获取第一个输入并在内部存储数据。 In order to get more data, you call operator++. 为了获得更多数据,您可以调用operator ++。 So here's what's happening in your code: 所以这是你的代码中发生的事情:

int main()
{

    istream_iterator<string>ii(cin);  // gets the first string "Hello"
    istream_iterator<int>jj(cin); // tries to get an int, but fails and puts cin in an error state

    string s1 = *ii; // stores "Hello" in s1
    ++ii;            // Tries to get the next string, but can't because cin is in an error state
    string s2 = *ii; // stores "Hello" in s2
    int i1 = *jj;    // since the previous attempt to get an int failed, this gets the default value, which is 0

    cout <<s1 << ' '<<s2 <<' '<< i1 << '\n';
}

Here's what you want to do: 这是你想要做的:

int main()
{

    istream_iterator<string>ii(cin);

    string s1 = *ii;
    ++ii;
    string s2 = *ii;

    istream_iterator<int>jj(cin);
    int i1 = *jj;

    // after this, you can use the iterators alternatingly,
    //  calling operator++ to get the next input each time

    cout <<s1 << ' '<<s2 <<' '<< i1 << '\n';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM