c语言cin读取一行txt,如何在C+中逐行迭代CIN?

我写的是LineInputIterator(写成一个练习,但也许有一天会有用):#ifndef UB_LINEINPUT_ITERATOR_H#define UB_LINEINPUT_ITERATOR_H#include #include #include 

#include namespace ub {template class LineInputIterator :

public std::iterator<:input_iterator_tag>{public:

typedef typename StringT::value_type char_type;

typedef typename StringT::traits_type traits_type;

typedef std::basic_istream istream_type;

LineInputIterator(): is(0) {}

LineInputIterator(istream_type& is): is(&is) {}

const StringT& operator*() const { return value; }

const StringT* operator->() const { return &value; }

LineInputIterator& operator++()

{

assert(is != NULL);

if (is && !getline(*is, value)) {

is = NULL;

}

return *this;

}

LineInputIterator operator++(int)

{

LineInputIterator prev(*this);

++*this;

return prev;

}

bool operator!=(const LineInputIterator& other) const

{

return is != other.is;

}

bool operator==(const LineInputIterator& other) const

{

return !(*this != other);

}private:

istream_type* is;

StringT value;};} // end ub#endif

因此,您的循环可以替换为一个算法(C+中的另一个推荐做法):for_each(LineInputIterator<>(cin), LineInputIterator<>(), do_stuff);

也许一个常见的任务是将每一行存储在一个容器中:vector lines((LineInputIterator<>(stream)), LineInputIterator<>());

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值