【问题标题】:Iterators without STL containers没有 STL 容器的迭代器
【发布时间】:2020-05-29 09:32:01
【问题描述】:

起初对我的英语感到抱歉。 我m preparing for my exam and I need to practice iterators, that dont 使用容器。 我想在 '\n' 之类的符号旁边输入一些符号,但我不知道该怎么做。 问题是我无法停止输入符号。 希望您能够帮助我。 衷心感谢您!


#include <iostream>
#include <iterator>
#include <algorithm>
#include <fstream>
using namespace std;
int main()
{
    ofstream out("name.txt");
    istream_iterator<char> it(cin);
    istream_iterator<char> end_of_stream;
    ostream_iterator<char> fout(out, " ");
    copy (it, end_of_stream, fout);
}

【问题讨论】:

  • 您正在迭代直到“流结束”,因此您必须提供它。通常您可以使用 来执行此操作。 unix.stackexchange.com/questions/110240/…
  • @SimonKraemer 关键是 OP 不想迭代到流的末尾。 I would like to input some symbols right to the symbol like '\n'
  • @john The problem is I am not able to stop entering symbols. 但也许我误解了这个问题。我只写评论而不是答案是有原因的。
  • @SimonKraemer 够公平的。我之所以发表评论,是因为该问题已作为与您所做的相同解释的重复而被关闭。
  • 谢谢你们,你们帮了我很多!

标签: c++ iterator


【解决方案1】:

在获得特定值(我认为)之前没有要复制的算法,因此您必须编写自己的循环。

这样

while (it != end_of_stream && *it != '\n')
{
    fout << *it;
    ++it;
}

【讨论】:

    猜你喜欢
    • 2013-04-16
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-05
    • 2010-11-26
    • 2011-01-15
    • 2014-09-10
    相关资源
    最近更新 更多